@Test
public void text() throws ParseException {
//将“1996-08-08T16:00:00.000Z”UTC时间转换成北京时间
String cBirth = "1996-08-08T16:00:00.000Z";
cBirth = cBirth.replace("Z"," UTC");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS Z");
//此时输出为:Fri Aug 09 00:00:00 CST 1996
System.out.println(sdf.parse(cBirth));
//将"yyyy-MM-dd"格式时间字符串转化成Data类型
String data ="2019-08-23";
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd");//字符串格式
//输出结果为:Fri Aug 23 00:00:00 CST 2019
System.out.println(sdf2.parse(data));
//Date时间格式转换成字符串输出
Timestamp a = new Timestamp(System.currentTimeMillis());//数据库DateTime类型对应
Date d = new Date();
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//输出格式
System.out.println(sdf1.format(d));
System.out.println(sdf1.format(a));
//在原时间上加上一天
Date now = new Date();
Calendar c = Calendar.getInstance();
c.setTime(now);
c.add(Calendar.DAY_OF_MONTH, 1);
Date tomorrow = c.getTime();
System.out.println(now);
System.out.println(tomorrow);
}
Java-各种时间格式转换
最新推荐文章于 2026-04-23 16:19:18 发布
本文详细介绍了使用Java进行日期和时间的操作方法,包括如何将UTC时间转换为北京时间、如何将字符串转换为Date类型以及如何将Date类型转换为字符串。此外,还介绍了如何在原时间基础上加一天的具体实现。
7611

被折叠的 条评论
为什么被折叠?



