Java 常见的日期转换(Timestamp、Calendar、Date、String)
日期类型:文章来源地址https://www.toymoban.com/news/detail-765461.html
- 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();
文章来源:https://www.toymoban.com/news/detail-765461.html
到了这里,关于Java 常见的日期转换(Timestamp、Calendar、Date、String)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!