我从这样的 json 中的 PostgreSQL 服务器获取时区时间(没有日期组件){ "time": "03:00:00+01" }
。我如何在 Android 中处理这个问题?有没有什么结构可以保持没有日期的时间?或者将其转换为纪元Date
表示,即Thu Jan 01 03:00:00 GMT+01:00 1970
是唯一好的解决方案?
3 回答
来自 java.time 和 ThreeTenABP 的 OffsetTime
AnOffsetTime
是没有日期且与 UTC 有偏移的一天中的时间。因此,它非常精确地从 JSON 中对字符串中的信息进行建模。所以我显然更喜欢它Date
。还因为Date
该类设计不良且早已过时。
String timeStringFromJson = "03:00:00+01";
DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("HH:mm:ssX");
OffsetTime parsedTime = OffsetTime.parse(timeStringFromJson, timeFormatter);
System.out.println("Parsed time: " + parsedTime);
此片段的输出是:
解析时间:03:00+01:00
作为一个对您来说可能或可能无关紧要的细节,字符串的偏移量被保留,这与Date
由于 aDate
没有时区或偏移量而可以做的相反。
问题:java.time 不需要 Android API 级别 26 吗?
java.time 在较旧和较新的 Android 设备上都能很好地工作。它只需要至少Java 6。
- 在 Java 8 及更高版本以及更新的 Android 设备(从 API 级别 26 开始)中,现代 API 是内置的。
- 在非 Android Java 6 和 7 中获得 ThreeTen Backport,现代类的后向端口(ThreeTen 用于 JSR 310;请参阅底部的链接)。
- 在(较旧的)Android 上使用 ThreeTen Backport 的 Android 版本。它被称为 ThreeTenABP。并确保从
org.threeten.bp
子包中导入日期和时间类。
链接
- Oracle 教程:日期时间解释如何使用 java.time。
- Java 规范请求 (JSR) 310,
java.time
第一次描述的地方。 - ThreeTen Backport 项目,
java.time
Java 6 和 7 的反向移植(ThreeTen for JSR-310)。 - ThreeTenABP , ThreeTen Backport 的 Android 版
- 问题:如何在Android项目中使用ThreeTenABP,有非常详尽的解释。
You can convert the String timestamp to the milliseconds since Epoch as a long
value, then store/retrieve the long
value and convert it back to a String timestamp as done below in main
.
I'm assuming the +01
in your example is for Central European Time.
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;
public class SDFTest {
/**
Global SimpleDateFormat
*/
private static SimpleDateFormat sdf = null;
public static void main(String[] args) {
// Initialize the Simple Date Format;
sdf = new SimpleDateFormat("HH:mm:ssX");
// Ignore this if you need timestamps converted to your local TimeZone;
sdf.setTimeZone(TimeZone.getTimeZone("CET"));
// Say this is the supplied time String;
String suppliedDate = "03:00:00+01";
// Convert from String timestamp to long;
long convertedToTimeStamp = getTimeStamp(suppliedDate);
System.out.println(suppliedDate + " => " + convertedToTimeStamp + "ms");
// Convert from long timestamp to String;
String convertedBackToString = getDateString(convertedToTimeStamp);
System.out.println(convertedToTimeStamp + "ms => " + convertedBackToString);
}
/**
Converts String timestamp to the number of milliseconds since Epoch.
*/
private static long getTimeStamp(String date_str) {
try {
Date date = sdf.parse(date_str);
return date.getTime();
} catch (Exception e) {
e.printStackTrace();
}
return 0;
}
/**
Converts long timestamp to String
*/
private static String getDateString(long timestamp) {
try {
Date date = new Date(timestamp);
return sdf.format(date);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
Output:
As the Date component is not present in the suppliedDate
it won't ever matter.
您可以使用以下模式将日期解析为Date
对象:SimpleDateFormat
SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
try
{
Date date = format.parse("03:00:00+01", Locale.getDefault());
Log.d( "debugDate", " date is: " + date );
}
catch (ParseException e)
{
e.printStackTrace();
}
输出:
Thu Jan 01 03:00:00 GMT+01:00 1970
如果您的日期结构变为“03:00:00+01:00”,则需要使用此模式:
SimpleDateFormat format = new SimpleDateFormat("HH:mm:ssZ", Locale.getDefault());
输出:
Thu Jan 01 03:00:00 GMT+01:00 1970