简体   繁体   English

如何使用时区转换日期字符串

[英]How to convert the date String with timezone

I have some string with date format such as我有一些日期格式的字符串,例如

Sat Aug 15 13:53:41 MYT 2015

I use SimpleDateFormate and tried to convert the String type into Date type.我使用 SimpleDateFormate 并尝试将 String 类型转换为 Date 类型。 It converted the String type into Date type, however, the timezone "MYT" is not recognized by Java and instead it was set to the default timezone of my computer ie "CST".它将 String 类型转换为 Date 类型,但是,Java 无法识别时区“MYT”,而是将其设置为我计算机的默认时区,即“CST”。 I need the time zone to be record in the Date object.我需要在 Date 对象中记录时区。 How could I do it?我怎么能做到?

If you know the time zone in advance, you can hardcode it into the formatter like this:如果您提前知道时区,您可以像这样将其硬编码到格式化程序中:

SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
format.setTimeZone(TimeZone.getTimeZone("Asia/Kuala_Lumpur"));
Date date = format.parse("2016-01-01 00:00:00 MYT");

You can follow the code你可以按照代码

String dateStr = "Sat Aug 15 13:53:41 MYT 2015";
DateFormat formatter = new SimpleDateFormat("E MMM dd HH:mm:ss Z yyyy");
Date date = (Date)formatter.parse(dateStr);
System.out.println(date);        

Calendar cal = Calendar.getInstance();
cal.setTime(date);
String formatedDate = cal.get(Calendar.DATE) + "/" + (cal.get(Calendar.MONTH) + 1) + "/" +         cal.get(Calendar.YEAR);

System.out.println("formatedDate : " + formatedDate); 

Resource Link:资源链接:

  1. How to convert “ Mon Jun 18 00:00:00 IST 2012” to 18/06/2012? 如何将“Mon Jun 18 00:00:00 IST 2012”转换为 18/06/2012?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM