Java 获取设备的本地时区
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4312680/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting device's local timezone
提问by xydev
2010-06-14 02:21:49+0400 or 2010-06-14 02:21:49-0400
2010-06-14 02:21:49+0400 或 2010-06-14 02:21:49-0400
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
有没有办法根据格式为 2010-06-14 02:21 AM 的本地机器时区将此字符串转换为日期
采纳答案by Buhake Sindi
Adding to what @org.life.java and @Erica said, here's what you should do
添加@org.life.java 和@Erica 所说的,这是你应该做的
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);
Then newDateStr
will be your new date formatted string.
然后newDateStr
将是您的新日期格式字符串。
UPDATE@xydev, the example I gave you works, see the full source code below:
更新@xydev,我给你的例子有效,请参阅下面的完整源代码:
/**
*
*/
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();
}
}
}
Output: 2010-06-14 08:21:49 AM
输出: 2010-06-14 08:21:49 AM
回答by Jigar Joshi
Using SimpleDateFormat
String string1 = "2010-06-14 02:21:49-0400";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ssZ")
sdf.setTimeZone(tz);
Date date = sdf.parse(string1);
Note: I am not sure the same class is available in andriod.
注意:我不确定在 andriod 中是否有相同的类。
回答by Erica
You can do all sorts of fancy formatting and localisation of dates using the DateFormat class. There's very good, complete documentation at the start of the API page here: http://download.oracle.com/javase/1.4.2/docs/api/java/text/DateFormat.html
您可以使用 DateFormat 类对日期进行各种花哨的格式化和本地化。API 页面的开头有非常好的、完整的文档:http: //download.oracle.com/javase/1.4.2/docs/api/java/text/DateFormat.html
Most regular cases can be handled with the built in SimpleDateFormat object. Its details are here: http://download.oracle.com/javase/1.4.2/docs/api/java/text/SimpleDateFormat.html
大多数常规情况可以使用内置的 SimpleDateFormat 对象处理。它的详细信息在这里:http: //download.oracle.com/javase/1.4.2/docs/api/java/text/SimpleDateFormat.html
The SimpleDateFormat output pattern string for the example you have above would be:
您上面的示例的 SimpleDateFormat 输出模式字符串将是:
yyyy-MM-dd hh:mm a
回答by Zvi
Be careful that if you are running on the emulator your timezone is always GMT. I just spent 2 hours in trying to understand why my application does not give me the right time (the one my computer displays) until I realized it is because of the emulator.
请注意,如果您在模拟器上运行,您的时区始终为 GMT。我只是花了 2 个小时试图理解为什么我的应用程序没有给我正确的时间(我的计算机显示的时间),直到我意识到这是因为模拟器。
To check you are running on the emulator use
要检查您是否在模拟器上运行,请使用
if (Build.PRODUCT.contains("sdk")){
// your code here for example if curtime has the emulator time
// since midnight in milliseconds then
curtime += 2 * 60 * 60 * 1000; // to add 2 hours from GMT
}