Java 常见的日期转换(Timestamp、Calendar、Date、String)

本文详细介绍了在Java中如何进行Timestamp、Calendar、Date和String之间的相互转换,包括12种常见的转换操作,并提到了日期转换工具类DateConverterUtil。
摘要由CSDN通过智能技术生成

Java 常见的日期转换(Timestamp、Calendar、Date、String)

日期类型:

  • Timestamp: java.sql.Timestamp
  • Calendar: java.util.Calendar
  • Date: java.util.Date
  • String: java.lang.String

1、Timestamp 转 Calendar

		Timestamp timestamp = Timestamp.valueOf("2023-06-06 21:39:52");
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(timestamp);

2、Timestamp 转 Date

		Timestamp timestamp = Timestamp.valueOf("2023-06-06 21:39:52");
		long time = timestamp.getTime();
		Date date = new Date(time);

3、Timestamp 转 String

		Timestamp timestamp = Timestamp.valueOf("2023-06-06 21:39:52");
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String dateStr = sdf.format(timestamp);

4、Calendar 转 Timestamp

		Calendar calendar = Calendar.getInstance();
		long time = calendar.getTimeInMillis();
		Timestamp timestamp = new Timestamp(time);

5、Calendar 转 Date

		Calendar calendar = Calendar.getInstance();
		Date date = calendar.getTime();

6、Calendar 转 String

		Calendar calendar = Calendar.getInstance();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String dateStr = sdf.format(calendar.getTime());

7、Date 转 Calendar

		Date date = new Date();
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);

8、Date 转 Timestamp

		Date date = new Date();
		long time = date.getTime();
		Timestamp timestamp = new Timestamp(time);

9、Date 转 String

		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值