600字范文,内容丰富有趣,生活中的好帮手!
600字范文 > java字符串转时间戳_java时间戳与字符串相互转换

java字符串转时间戳_java时间戳与字符串相互转换

时间:2021-07-21 02:08:18

相关推荐

java字符串转时间戳_java时间戳与字符串相互转换

第一步:创建工具类

/**

* @data on /9/3 10:51 AM

* @auther

* @describe java 时间戳/字符串 之间转换

*/

public class DateUtil {

private static SimpleDateFormat sf = null;

// 将时间戳转成字符串

public static String getDateToString(long time) {

Date d = new Date(time);

sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

return sf.format(d);

}

//获取当前时间

public static String getCurrentDate() {

Date d = new Date();

sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

return sf.format(d);

}

//将字符串转换为时间戳

public static long getStringToDate(String time) {

sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

Date date = new Date();

try {

date = sf.parse(time);

} catch (ParseException e) {

e.printStackTrace();

}

return date.getTime();

}

//直接获取当前时间戳

public static String getTimeStamp() {

String currentDate = getCurrentDate();

sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

Date date = new Date();

try {

date = sf.parse(currentDate);

} catch (ParseException e) {

e.printStackTrace();

}

return String.valueOf(date.getTime());

}

}

然后,调用就完事儿了!

第二步:xml布局文件展示

xmlns:app="/apk/res-auto"

xmlns:tools="/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

tools:context=".show.Case25"

tools:ignore="MissingConstraints">

android:id="@+id/toolbar"

android:layout_width="match_parent"

android:layout_height="?attr/actionBarSize"

android:background="@color/green"

app:title="java时间戳转字符串"

app:titleTextColor="@color/white" />

android:id="@+id/btn_show_time_chuo"

android:layout_width="match_parent"

app:layout_constraintTop_toBottomOf="@+id/toolbar"

android:layout_height="wrap_content"

android:text="获取当前时间戳" />

android:id="@+id/btn_show_time"

android:layout_width="match_parent"

app:layout_constraintTop_toBottomOf="@+id/btn_show_time_chuo"

android:layout_height="wrap_content"

android:text="获取当前时间" />

android:id="@+id/btn_change"

android:layout_width="match_parent"

app:layout_constraintTop_toBottomOf="@+id/btn_show_time"

android:layout_height="wrap_content"

android:text="时间戳转字符串" />

android:id="@+id/btn_change2"

android:layout_width="match_parent"

app:layout_constraintTop_toBottomOf="@+id/btn_change"

android:layout_height="wrap_content"

android:text="字符串转时间戳" />

android:id="@+id/tv_show"

android:layout_marginTop="40dp"

android:background="@color/blue"

android:textColor="@color/white"

android:textSize="20sp"

android:gravity="center"

tools:text="测试一下"

app:layout_constraintTop_toBottomOf="@id/btn_change2"

android:layout_width="match_parent"

android:layout_height="40dp"/>

第三步:调用工具类中的方法,完成转换

public class Case25 extends AppCompatActivity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_case25);

Button getTimeChuo = findViewById(R.id.btn_show_time_chuo);

Button getTime = findViewById(R.id.btn_show_time);

Button changeToTime = findViewById(R.id.btn_change);

Button changeToChuo = findViewById(R.id.btn_change2);

TextView tvShow = findViewById(R.id.tv_show);

getTimeChuo.setOnClickListener((View)->{

tvShow.setText(DateUtil.getTimeStamp());

});

getTime.setOnClickListener((View)->{

tvShow.setText(DateUtil.getCurrentDate());

});

changeToTime.setOnClickListener((View)->{

tvShow.setText(DateUtil.getDateToString(1600074914));

});

changeToChuo.setOnClickListener((View)->{

//String.valueof(Long):long转成String类型

tvShow.setText(String.valueOf(DateUtil.getStringToDate("-09-18 18:00:00")));

});

}

}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。