我在应用程序中显示日期时遇到问题。
我得到的时间戳为:
2017-08-02T06:05:30.000 Z
但是根据这个实际时间是:
2017:08:02 11:35 AM
但是使用我的代码转换后,它显示时间为:
6:00 am
如何将其显示为当前时间?
我的代码如下:
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.p(time);
} catch (PException e) {
e.printStackTrace();
}
String formattedTime = getSdftimeformat().format(date1);
return formattedTime;
}
首先,您在格式中使用mm:ss.mmm
。根据SimpleDateFormat
javadoc,m
表示分钟,因此您必须将其更改为mm:ss.SSS
,因为S
表示毫秒。
另一个细节是最后的Z
是timezone designator for UTC,它不能被忽略(至少它不应该)。
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSX");
Date date = sdf.p("2017-08-02T06:05:30.000Z");
PS:X
模式是在 Java 7 中引入的。如果您使用的是 Java & lt;= 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.p("2017-08-02T06:05:30.000Z");
要将时间格式化为您的时区,您必须使用另一个具有相应时区的SimpleDateFormat
:
// output format: hour:minute AM/PM
SimpleDateFormat outputFormat = new SimpleDateFormat("hh:mm a", Locale.ENGLISH);
// uming a timezone in India
outputFormat.setTimeZone(TimeZone.getTimeZone("Asia/Kolkata"));
System.out.println(outputFormat.format(date));
输出将是:
11:35 AM
如果你不设置时区,它将使用系统的默认值。但是默认值可以在没有通知的情况下更改,即使在运行时,所以最好明确设置一个特定的时区。
我还使用java.util.Locale
将语言设置为英语,因为某些区域设置可以为 AM / PM 使用不同的符号。如果您不指定一个,它将使用系统默认值,并且不能保证其中的符号是您需要的符号(某些区域设置使用“a.m./ p.m.”或其他不同的格式,因此最好使用显式区域设置)。
Java 新的日期 / 时间 API
旧类(Date
,Calendar
和SimpleDateFormat
)有lots of problems和design issues,它们正在被新的 API 所取代。
如果您使用的是Java 8,请考虑使用new java.time API。更简单的是less bugged and less error-e than the old APIs。
如果您使用的是Java & lt;= 7,则可以使用ThreeTen Backport,这是 Java 8 的新日期 / 时间类的一个很好的 backport。对于Android,有ThreeTenABP(有关如何使用它的更多信息here)。
下面的代码适用于两者。唯一的区别是包名(在 Java 8 中是java.time
,在 ThreeTen Backport(或 Android 的 ThreeTenABP)中是org.threeten.bp
),但类和方法名称是相同的。
要解析输入,您可以使用ZonedDateTime
类,它完全支持时区,并且可以很容易地转换到另一个区域。然后使用DateTimeFormatter
格式化输出:
// p the input
ZonedDateTime pd = ZonedDateTime.p("2017-08-02T06:05:30.000Z");
// convert to another timezone
ZonedDateTime z = pd.withZoneSameInstant(ZoneId.of("Asia/Kolkata"));
// format output
DateTimeFormatter fmt = DateTimeFormatter.ofPattern("hh:mm a", Locale.ENGLISH);
System.out.println(fmt.format(z));
输出将是:
11:35 AM
如果输入总是最后有Z
,您也可以使用Instant
类:
// p the input
Instant instant = Instant.p("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
-检查javadoc了解更多详细信息。
另请注意,API 使用IANA timezones names(始终采用Region/City
格式,如Asia/Kolkata
或Europe/Berlin
)。避免使用 3 个字母的缩写(如CST
或IST
),因为它们是ambiguous and not standard。
您可以通过调用ZoneId.getAvailableZoneIds()
获取可用时区列表(并选择最适合您的系统的时区)。
您也可以使用ZoneId.systemDefault()
系统的默认时区,但这可以在没有通知的情况下更改,即使在运行时,所以最好明确使用特定的一个。
您需要使用 SimpleDateFormat 类并指定要解析的格式,如下所示:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss", Locale.getDefault());
long timeStamp = sdf.p('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
试试这个你会得到结果
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 (PException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String localtime = df.format(cal.getTime());
使用这个获取当前时间。
Calendar cal =
Calendar.getInstance(TimeZone.getTimeZone("T+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("T+5:30"));
String localTime = date.format(currentLocalTime);
将时区更改为您的时区
本站系公益性非盈利分享网址,本文来自用户投稿,不代表码文网立场,如若转载,请注明出处
评论列表(69条)