获取设备的本地时区 [英] Getting device's local timezone
本文介绍了获取设备的本地时区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
2010-06-14 02:21:49 + 0400或2010-06-14 02:21:49-0400
2010-06-14 02:21:49+0400 or 2010-06-14 02:21:49-0400
是有办法根据与格式2010-06-14上午02时21
is there a way to convert this string to the date according to the local machine time zone with format 2010-06-14 02:21 AM
推荐答案
添加到什么@ org.life.java和@Erica说,这里是你应该做的。
Adding to what @org.life.java and @Erica said, here's what you should do
String dateStr = "2010-06-14 02:21:49-0400";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ssZ");
TimeZone tz = TimeZone.getDefault();
sdf.setTimeZone(tz);
Date date = sdf.parse(dateStr);
sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss a");
String newDateStr = sdf.format(date);
System.out.println(newDateStr);
然后 newDateStr
将是您新的日期格式的字符串。
Then newDateStr
will be your new date formatted string.
更新 @xydev,我给你的工作示例,请参见下面的完整源$ C $ C:
UPDATE @xydev, the example I gave you works, see the full source code below:
/**
*
*/
package testcases;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
/**
* @author The Elite Gentleman
*
*/
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
String dateStr = "2010-06-14 02:21:49-0400";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ssZ");
TimeZone tz = TimeZone.getDefault();
sdf.setTimeZone(tz);
Date date = sdf.parse(dateStr);
sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss a");
String newDateStr = sdf.format(date);
System.out.println(newDateStr);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
输出: 2010-06-14上午08点21分49秒
这篇关于获取设备的本地时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文