java Android 将中部时间转换为当地时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2407719/
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
Android Convert Central Time to Local Time
提问by northdig
I have a MySql database that stores a timestamp for each record I insert. I pull that timestamp into my Android application as a string. My database is located on a server that has a TimeZone of CST. I want to convert that CST timestamp to the Android device's local time.
我有一个 MySql 数据库,它为我插入的每条记录存储一个时间戳。我将该时间戳作为字符串提取到我的 Android 应用程序中。我的数据库位于具有 CST 时区的服务器上。我想将该 CST 时间戳转换为 Android 设备的本地时间。
Can someone help with this?
有人可以帮忙吗?
采纳答案by Sephy
can't you simply convert the date with simpleDateFormat? then you just define the structure of your incoming date like that (df) and transform it to the form you want (df):
你不能简单地用simpleDateFormat转换日期吗?然后你只需定义传入日期的结构(df)并将其转换为你想要的形式(df):
private static DateFormat df = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z");
private static DateFormat df2 = new SimpleDateFormat("dd/MM/yyyy 'at' HH:mm");
public void setyourDate(String yourDate) {
Date date2;
yourDate = getyourDate() + "" + yourDate;
try {
date2 = df.parse(yourDate);
yourDate = df2.format(date2);
} catch (ParseException e) {
}
this.yourDate = yourDate;
}
does it make sense?
是否有意义?
回答by Anthony
Use getTimeZone.getDefaultcombined with according to the Android documentation.
使用getTimeZone.getDefault具有根据合并Android文档。
public static synchronized TimeZone getDefault ()
Gets the default time zone. Returns the default time zone.
公共静态同步时区 getDefault()
获取默认时区。返回默认时区。
So since you know that CST is -6:00 from GMT, and you get a local timezone saying the user is +9:00 (Japan), you'd know to adjust your MySQL DB times by +15 hours (9 - (-6)). Or if they are in Miami (EST, -5), you would adjust by adding one hour (-5 - (-6)). If the are in Portland, Oregon, (PST -8), you would subtract 2 hours (-8 -(-6)).
因此,既然您知道 CST 是格林威治标准时间的 -6:00,并且您得到本地时区说用户是 +9:00(日本),那么您就会知道将 MySQL 数据库时间调整 +15 小时(9 - ( -6))。或者,如果他们在迈阿密(东部标准时间,-5),您可以通过增加一小时来调整(-5 - (-6))。如果在俄勒冈州波特兰 (PST -8),您将减去 2 小时 (-8 -(-6))。
So really you just need to get the local timezone offset and feed it into the basic equation: TimeZone.getDefault + 6and you'll know what to add or subtract to your local DB. (+6 since -(-6) always works out to +6).
因此,实际上您只需要获取本地时区偏移量并将其输入基本等式: TimeZone.getDefault + 6您就会知道向本地数据库添加或减去什么。(+6 因为 -(-6) 总是 +6)。
If I knew the first thing about writing Java, I'd go the extra step and write a bit of sample code, but alas, I'm only smart enough for scripts.
如果我知道编写 Java 的第一件事,我会多走一步并编写一些示例代码,但是唉,我只对脚本足够聪明。
Crude Attempt at Java
对 Java 的粗暴尝试
I already said I have no idea how to do Java or object oriented anything, right?
我已经说过我不知道如何做 Java 或面向对象的任何东西,对吗?
Here's a crude attempt from just poking around the Android documentation. Any fine points or simple "Not even close" remarks welcome. Bear in mind that I figured out the right method and class already just from a quick search and I came up with a simple equation for converting the timezone offset for anywhere to CST, so I'm not a dunce, just someone who doesn't know when to leave well enough alone. Anyway, crude attempt:
这是一个粗略的尝试,只是通过浏览 Android 文档。欢迎任何细节或简单的“甚至不接近”的评论。请记住,我已经通过快速搜索找到了正确的方法和类,我想出了一个简单的等式,用于将任何地方的时区偏移量转换为 CST,所以我不是笨蛋,只是一个不知道的人知道什么时候离开足够好。无论如何,粗略的尝试:
System now = System.currentTimeMillis (); //Gets current local time in ms
TimeZone local_tz = TimeZone.getDefault(); //Gets current local TZ of phone
tz_offset_gmt = local_tz.getOffset(now)/3600000; // Get Offset in ms, divide by 3600000
tz_offset_cst = tz_offset_gmt + 6; // add offset to 6 to get current TZ offset to CST.
Anywhere close to how to do this in java?
任何接近如何在 Java 中执行此操作的地方?
回答by David
Suppose you have a string of date in CST, parse it with timezone CST and then format it with the default timezone on your android.
假设您在 CST 中有一个日期字符串,使用时区 CST 解析它,然后使用您的 android 上的默认时区对其进行格式化。
String s = "2011-01-01 12:00:00";
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
df.setTimeZone(TimeZone.getTimeZone("CST"));
Date timestamp = null;
try {
timestamp = df.parse(s);
df.setTimeZone(TimeZone.getDefault());
System.out.println(df.format(timestamp));
} catch (ParseException e) {
e.printStackTrace();
}
回答by Alexander Melnikov
This is an old question, but I want to write my answer. Assume, the timestamp you get from SQL is like the following format: yyyy-MM-dd'T'HH:mm:ss
这是一个老问题,但我想写下我的答案。假设,你从 SQL 得到的时间戳格式如下:yyyy-MM-dd'T'HH:mm:ss
public static Date convertStringToDate(String strDate) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
sdf.setTimeZone(TimeZone.getTimeZone("CST"));
return sdf.parse(strDate);
}

