我在申请表中显示日期时遇到问题。
我的时间戳为:
2017-08-02T06:05:30.000Z
但就此而言,实际时间是:
2017:08:02 11:35 AM
但是在使用我的代码转换后,它将时间显示为:
早上6点
如何将其显示为当前时间?
我的代码如下:
private static SimpleDateFormat timestampformat =
new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.mmm'Z'");
private static SimpleDateFormat sdftimeformat = new SimpleDateFormat("HH:mm a");
private static SimpleDateFormat getSdftimeformat() {
return sdftimeformat;
}
public static String timeStampConvertToTime(String time) {
Date date1 = null;
try {
date1 = timestampformat.parse(time);
} catch (ParseException e) {
e.printStackTrace();
}
String formattedTime = getSdftimeformat().format(date1);
return formattedTime;
}
答案 0 :(得分:6)
首先,您在格式中使用mm:ss.mmm
。根据{{3}},m
表示分钟,因此您必须将其更改为mm:ss.SSS
,因为S
表示毫秒。
另一个细节是Z
到底是SimpleDateFormat
javadoc并且它不能被忽略(至少它不应该被忽略)。您必须使用相应的模式,即X
:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSX");
Date date = sdf.parse("2017-08-02T06:05:30.000Z");
PS:在Java 7中引入了X
模式。如果您使用Java< = 6,唯一的选择是将Z
视为文字(一个丑陋的解决方法,我承认)并将UTC设置为解析器使用的时区:
// treat "Z" as literal
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
// use UTC as timezone
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
Date date = sdf.parse("2017-08-02T06:05:30.000Z");
这样,日期将具有与UTC中06:05对应的值。要将时间格式化为您的时区,您必须使用具有相应时区的其他SimpleDateFormat
:
// output format: hour:minute AM/PM
SimpleDateFormat outputFormat = new SimpleDateFormat("hh:mm a", Locale.ENGLISH);
// assuming a timezone in India
outputFormat.setTimeZone(TimeZone.getTimeZone("Asia/Kolkata"));
System.out.println(outputFormat.format(date));
输出将是:
上午11:35
如果您没有设置时区,它将使用系统的默认值。但是默认值可以在没有通知的情况下进行更改,即使在运行时也是如此,因此最好明确设置一个特定的时区,如上所述。
我还使用java.util.Locale
将语言设置为英语,因为某些语言环境可以为AM / PM设置不同的符号。如果你没有指定一个,它将使用系统默认值,并且不保证符号是你需要的符号(一些语言环境使用“am / pm”或其他不同的格式,所以最好使用一个明确的语言环境)。
旧类(Date
,Calendar
和SimpleDateFormat
)有timezone designator for UTC和lots of problems,它们将被新API取代。< / p>
如果您使用的是 Java 8 ,请考虑使用design issues。它更容易,new java.time API。
如果您使用的是 Java&lt; = 7 ,则可以使用less bugged and less error-prone than the old APIs,这是Java 8新日期/时间类的绝佳后端。对于 Android ,有ThreeTen Backport(更多关于如何使用它ThreeTenABP)。
以下代码适用于两者。
唯一的区别是包名称(在Java 8中为java.time
,在ThreeTen Backport(或Android的ThreeTenABP)中为org.threeten.bp
),但类和方法名称是相同的
要解析输入,您可以使用ZonedDateTime
类,该类完全支持时区,并且可以非常轻松地转换到其他区域。然后使用DateTimeFormatter
格式化输出:
// parse the input
ZonedDateTime parsed = ZonedDateTime.parse("2017-08-02T06:05:30.000Z");
// convert to another timezone
ZonedDateTime z = parsed.withZoneSameInstant(ZoneId.of("Asia/Kolkata"));
// format output
DateTimeFormatter fmt = DateTimeFormatter.ofPattern("hh:mm a", Locale.ENGLISH);
System.out.println(fmt.format(z));
输出将是:
上午11:35
如果输入总是最后有Z
,您还可以使用Instant
类:
// parse the input
Instant instant = Instant.parse("2017-08-02T06:05:30.000Z");
// convert to a timezone
ZonedDateTime z = instant.atZone(ZoneId.of("Asia/Kolkata"));
请注意,我在小时内使用了hh
:这将使用1到12之间的值进行格式化(这是有意义的,因为我还使用了AM / PM指示符)。如果您想要0到23之间的值,请使用HH
- 检查here以获取更多详细信息。
另请注意,API使用javadoc(始终采用Region/City
格式,例如Asia/Kolkata
或Europe/Berlin
。
避免使用3个字母的缩写(例如CST
或IST
),因为它们是IANA timezones names。
您可以致电ZoneId.getAvailableZoneIds()
获取可用时区列表(并选择最适合您系统的时区)。
您也可以将系统的默认时区与ZoneId.systemDefault()
一起使用,但即使在运行时也可以在不事先通知的情况下进行更改,因此最好明确使用特定的时区。
答案 1 :(得分:-1)
试试这个你的结果
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat(
"yyyy-MM-dd'T'HH:mm:ss'Z'");
// set your format in df variable
SimpleDateFormat df = new SimpleDateFormat(
"HH:mm a");
try {
cal.setTime('your value');
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String localtime = df.format(cal.getTime());
答案 2 :(得分:-1)
使用它来获取当前时间。
Calendar cal =
Calendar.getInstance(TimeZone.getTimeZone("GMT+5:30"));
Date currentLocalTime = cal.getTime();
DateFormat date = new SimpleDateFormat("HH:mm a");
// you can get seconds by adding "...:ss" to it
date.setTimeZone(TimeZone.getTimeZone("GMT+5:30"));
String localTime = date.format(currentLocalTime);
将时区更改为您的时区
答案 3 :(得分:-1)
您需要使用SimpleDateFormat类并指定要从中解析的格式,如下所示:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss", Locale.getDefault());
long timeStamp = sdf.parse('your_timestamp').getTime();
SimpleDateFormat currentDateFormat = new SimpleDateFormat("dd-MM-yyyy hh:mm a", Locale.getDefault());
String time =currentDateFormat.format(timeStamp); // Formatted time in string form
答案 4 :(得分:-1)
我认为Rose的时间戳中的Z是zulu时间,硬编码从zulu时间到他当地时区的转换并不正确(我们假设GMT + 5:30)。如果它总是返回Z可能没问题,但如果是的话 military time zones你需要能够处理所有可能时区的东西。
This previous question意味着没有内置方法可以做到这一点。需要了解时间戳来自哪里才能真正回答问题。