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

第一步:创建工具类

/**

* @data on 2020/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="http://schemas.android.com/apk/res-auto"

xmlns:tools="http://schemas.android.com/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("2020-09-18 18:00:00")));

});

}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值