时间格式处理

这篇博客介绍了如何使用Java进行日期时间格式转换,包括将"November26,202119:48"格式转换为"2021-11-2619:48",以及将国际时间"2021-12-13T01:10:10.0953Z"转换为北京时间"2021-12-1309:10:10.953"。内容涉及SimpleDateFormat类和日期时间解析。
摘要由CSDN通过智能技术生成

1、 November 26, 2021 19:48类型的时间格式处理

转换前:November 26, 2021 19:48   

转换后:2021-11-26 19:48
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class TestSj {

	public static void main(String[] args) throws ParseException {
		Date date = new Date();
		SimpleDateFormat dateFormat = new SimpleDateFormat("MMMMMMMM d, yyyy H:m", Locale.ENGLISH);
		String englishStr = dateFormat.format(date);
		System.out.println(englishStr);

		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		Date parseStr = dateFormat.parse(englishStr);
		String tempDate = df.format(parseStr);
		System.out.println(tempDate);
	}
}

2、2021-12-13T01:10:10.0953Z国际时间转为北京时间

转换前:2021-12-13T01:10:10.0953Z

转换后:2021-12-13 09:10:10.953
//转换前的时间
String dateTime = "2021-12-13T01:10:10.0953Z";
dateTime = dateTime.replace("Z", " UTC");
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS Z");
SimpleDateFormat defaultFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
try {
    Date time = format.parse(dateTime);

    //转换后的时间
    String result = defaultFormat.format(time);
    System.out.println(result);
} catch (Exception e) {
    e.printStackTrace();
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值