Java 将 GMT/UTC 转换为本地时间无法按预期工作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19375357/
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
Java Convert GMT/UTC to Local time doesn't work as expected
提问by Vinod Jayachandran
In Order to show a reproducible scenario, I am doing the following
为了显示可重现的场景,我正在执行以下操作
Get the current system time (local time)
Convert Local time to UTC // Works Fine Till here
Reverse the UTC time, back to local time. Followed 3 different approaches (listed below) but all the 3 approaches retains the time in UTC only.
{
long ts = System.currentTimeMillis(); Date localTime = new Date(ts); String format = "yyyy/MM/dd HH:mm:ss"; SimpleDateFormat sdf = new SimpleDateFormat (format); // Convert Local Time to UTC (Works Fine) sdf.setTimeZone(TimeZone.getTimeZone("UTC")); Date gmtTime = new Date(sdf.format(localTime)); System.out.println("Local:" + localTime.toString() + "," + localTime.getTime() + " --> UTC time:" + gmtTime.toString() + "-" + gmtTime.getTime()); // Reverse Convert UTC Time to Locale time (Doesn't work) Approach 1 sdf.setTimeZone(TimeZone.getDefault()); localTime = new Date(sdf.format(gmtTime)); System.out.println("Local:" + localTime.toString() + "," + localTime.getTime() + " --> UTC time:" + gmtTime.toString() + "-" + gmtTime.getTime()); // Reverse Convert UTC Time to Locale time (Doesn't work) Approach 2 using DateFormat DateFormat df = new SimpleDateFormat (format); df.setTimeZone(TimeZone.getDefault()); localTime = df.parse((df.format(gmtTime))); System.out.println("Local:" + localTime.toString() + "," + localTime.getTime() + " --> UTC time:" + gmtTime.toString() + "-" + gmtTime.getTime()); // Approach 3 Calendar c = new GregorianCalendar(TimeZone.getDefault()); c.setTimeInMillis(gmtTime.getTime()); System.out.println("Local Time " + c.toString());
}
获取当前系统时间(本地时间)
将本地时间转换为 UTC // 工作正常,直到这里
反转UTC时间,回到当地时间。遵循 3 种不同的方法(如下所列),但所有 3 种方法都仅保留 UTC 时间。
{
long ts = System.currentTimeMillis(); Date localTime = new Date(ts); String format = "yyyy/MM/dd HH:mm:ss"; SimpleDateFormat sdf = new SimpleDateFormat (format); // Convert Local Time to UTC (Works Fine) sdf.setTimeZone(TimeZone.getTimeZone("UTC")); Date gmtTime = new Date(sdf.format(localTime)); System.out.println("Local:" + localTime.toString() + "," + localTime.getTime() + " --> UTC time:" + gmtTime.toString() + "-" + gmtTime.getTime()); // Reverse Convert UTC Time to Locale time (Doesn't work) Approach 1 sdf.setTimeZone(TimeZone.getDefault()); localTime = new Date(sdf.format(gmtTime)); System.out.println("Local:" + localTime.toString() + "," + localTime.getTime() + " --> UTC time:" + gmtTime.toString() + "-" + gmtTime.getTime()); // Reverse Convert UTC Time to Locale time (Doesn't work) Approach 2 using DateFormat DateFormat df = new SimpleDateFormat (format); df.setTimeZone(TimeZone.getDefault()); localTime = df.parse((df.format(gmtTime))); System.out.println("Local:" + localTime.toString() + "," + localTime.getTime() + " --> UTC time:" + gmtTime.toString() + "-" + gmtTime.getTime()); // Approach 3 Calendar c = new GregorianCalendar(TimeZone.getDefault()); c.setTimeInMillis(gmtTime.getTime()); System.out.println("Local Time " + c.toString());
}
采纳答案by trylimits
I also recommend using Jodaas mentioned before.
我还建议使用前面提到的Joda。
Solving your problem using standard JavaDate
objects only can be done as follows:
只能使用标准JavaDate
对象解决您的问题,如下所示:
// **** YOUR CODE **** BEGIN ****
long ts = System.currentTimeMillis();
Date localTime = new Date(ts);
String format = "yyyy/MM/dd HH:mm:ss";
SimpleDateFormat sdf = new SimpleDateFormat(format);
// Convert Local Time to UTC (Works Fine)
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
Date gmtTime = new Date(sdf.format(localTime));
System.out.println("Local:" + localTime.toString() + "," + localTime.getTime() + " --> UTC time:"
+ gmtTime.toString() + "," + gmtTime.getTime());
// **** YOUR CODE **** END ****
// Convert UTC to Local Time
Date fromGmt = new Date(gmtTime.getTime() + TimeZone.getDefault().getOffset(localTime.getTime()));
System.out.println("UTC time:" + gmtTime.toString() + "," + gmtTime.getTime() + " --> Local:"
+ fromGmt.toString() + "-" + fromGmt.getTime());
Output:
输出:
Local:Tue Oct 15 12:19:40 CEST 2013,1381832380522 --> UTC time:Tue Oct 15 10:19:40 CEST 2013,1381825180000
UTC time:Tue Oct 15 10:19:40 CEST 2013,1381825180000 --> Local:Tue Oct 15 12:19:40 CEST 2013-1381832380000
回答by Tomasz Waszczyk
I strongly recommend using Joda Time http://joda-time.sourceforge.net/faq.html
我强烈建议使用 Joda Time http://joda-time.sourceforge.net/faq.html
回答by Basil Bourque
Joda-Time
乔达时间
UPDATE: The Joda-Timeproject is now in maintenance mode, with the team advising migration to the java.timeclasses. See Tutorial by Oracle.
更新:Joda-Time项目现在处于维护模式,团队建议迁移到java.time类。请参阅Oracle 教程。
See my other Answerusing the industry-leading java.timeclasses.
使用行业领先的java.time类查看我的其他答案。
Normally we consider it bad form on StackOverflow.com to answer a specific question by suggesting an alternate technology. But in the case of the date, time, and calendar classes bundled with Java 7 and earlier, those classes are so notoriously bad in both design and execution that I am compelled to suggest using a 3rd-party library instead: Joda-Time.
通常,我们认为在 StackOverflow.com 上通过建议替代技术来回答特定问题是不好的形式。但是对于与 Java 7 及更早版本捆绑的日期、时间和日历类,这些类在设计和执行方面都非常糟糕,我不得不建议改用第三方库:Joda-Time。
Joda-Time works by creating immutable objects. So rather than alter the time zone of a DateTime object, we simply instantiate a new DateTime with a different time zone assigned.
Joda-Time 通过创建不可变对象来工作。因此,与其更改 DateTime 对象的时区,不如简单地实例化一个具有不同时区的新 DateTime。
Your central concern of using both local and UTC time is so very simple in Joda-Time, taking just 3 lines of code.
您在 Joda-Time 中使用本地时间和 UTC 时间的核心问题非常简单,只需 3 行代码。
org.joda.time.DateTime now = new org.joda.time.DateTime();
System.out.println( "Local time in ISO 8601 format: " + now + " in zone: " + now.getZone() );
System.out.println( "UTC (Zulu) time zone: " + now.toDateTime( org.joda.time.DateTimeZone.UTC ) );
Output when run on the west coast of North America might be:
在北美西海岸运行时的输出可能是:
Local time in ISO 8601 format: 2013-10-15T02:45:30.801-07:00
Local time in ISO 8601 format: 2013-10-15T02:45:30.801-07:00
UTC (Zulu) time zone: 2013-10-15T09:45:30.801Z
UTC (Zulu) time zone: 2013-10-15T09:45:30.801Z
Here is a class with several examples and further comments. Using Joda-Time 2.5.
这是一个包含几个示例和进一步评论的课程。使用 Joda-Time 2.5。
/**
* Created by Basil Bourque on 2013-10-15.
* ? Basil Bourque 2013
* This source code may be used freely forever by anyone taking full responsibility for doing so.
*/
public class TimeExample {
public static void main(String[] args) {
// Joda-Time - The popular alternative to Sun/Oracle's notoriously bad date, time, and calendar classes bundled with Java 8 and earlier.
// http://www.joda.org/joda-time/
// Joda-Time will become outmoded by the JSR 310 Date and Time API introduced in Java 8.
// JSR 310 was inspired by Joda-Time but is not directly based on it.
// http://jcp.org/en/jsr/detail?id=310
// By default, Joda-Time produces strings in the standard ISO 8601 format.
// https://en.wikipedia.org/wiki/ISO_8601
// You may output to strings in other formats.
// Capture one moment in time, to be used in all the examples to follow.
org.joda.time.DateTime now = new org.joda.time.DateTime();
System.out.println( "Local time in ISO 8601 format: " + now + " in zone: " + now.getZone() );
System.out.println( "UTC (Zulu) time zone: " + now.toDateTime( org.joda.time.DateTimeZone.UTC ) );
// You may specify a time zone in either of two ways:
// ? Using identifiers bundled with Joda-Time
// ? Using identifiers bundled with Java via its TimeZone class
// ----| Joda-Time Zones |---------------------------------
// Time zone identifiers defined by Joda-Time…
System.out.println( "Time zones defined in Joda-Time : " + java.util.Arrays.toString( org.joda.time.DateTimeZone.getAvailableIDs().toArray() ) );
// Specify a time zone using DateTimeZone objects from Joda-Time.
// http://joda-time.sourceforge.net/apidocs/org/joda/time/DateTimeZone.html
org.joda.time.DateTimeZone parisDateTimeZone = org.joda.time.DateTimeZone.forID( "Europe/Paris" );
System.out.println( "Paris France (Joda-Time zone): " + now.toDateTime( parisDateTimeZone ) );
// ----| Java Zones |---------------------------------
// Time zone identifiers defined by Java…
System.out.println( "Time zones defined within Java : " + java.util.Arrays.toString( java.util.TimeZone.getAvailableIDs() ) );
// Specify a time zone using TimeZone objects built into Java.
// http://docs.oracle.com/javase/8/docs/api/java/util/TimeZone.html
java.util.TimeZone parisTimeZone = java.util.TimeZone.getTimeZone( "Europe/Paris" );
System.out.println( "Paris France (Java zone): " + now.toDateTime(org.joda.time.DateTimeZone.forTimeZone( parisTimeZone ) ) );
}
}
回答by albfan
You have a date with a known timezone (Here Europe/Madrid
), and a target timezone (UTC
)
您有一个已知时区的日期(此处Europe/Madrid
)和目标时区(UTC
)
You just need two SimpleDateFormats:
你只需要两个 SimpleDateFormats:
long ts = System.currentTimeMillis(); Date localTime = new Date(ts); SimpleDateFormat sdfLocal = new SimpleDateFormat ("yyyy/MM/dd HH:mm:ss"); sdfLocal.setTimeZone(TimeZone.getTimeZone("Europe/Madrid")); SimpleDateFormat sdfUTC = new SimpleDateFormat ("yyyy/MM/dd HH:mm:ss"); sdfUTC.setTimeZone(TimeZone.getTimeZone("UTC")); // Convert Local Time to UTC Date utcTime = sdfLocal.parse(sdfUTC.format(localTime)); System.out.println("Local:" + localTime.toString() + "," + localTime.getTime() + " --> UTC time:" + utcTime.toString() + "-" + utcTime.getTime()); // Reverse Convert UTC Time to Locale time localTime = sdfUTC.parse(sdfLocal.format(utcTime)); System.out.println("UTC:" + utcTime.toString() + "," + utcTime.getTime() + " --> Local time:" + localTime.toString() + "-" + localTime.getTime());
So after see it working you can add this method to your utils:
因此,在看到它工作后,您可以将此方法添加到您的工具中:
public Date convertDate(Date dateFrom, String fromTimeZone, String toTimeZone) 抛出 ParseException { 字符串模式 = "yyyy/MM/dd HH:mm:ss"; SimpleDateFormat sdfFrom = new SimpleDateFormat (pattern); sdfFrom.setTimeZone(TimeZone.getTimeZone(fromTimeZone)); SimpleDateFormat sdfTo = new SimpleDateFormat (pattern); sdfTo.setTimeZone(TimeZone.getTimeZone(toTimeZone)); 日期 dateTo = sdfFrom.parse(sdfTo.format(dateFrom)); 返回日期到; }
回答by Ole V.V.
I am joining the chtheitroad recommending that you skip the now long outdated classes Date
, Calendar
, SimpleDateFormat
and friends. In particular I would warn against using the deprecated methods and constructors of the Date
class, like the Date(String)
constructor you used. They were deprecated because they don't work reliably across time zones, so don't use them. And yes, most of the constructors and methods of that class are deprecated.
我加入唱诗班,建议您跳过现在早已过时的类Date
,Calendar
,SimpleDateFormat
和朋友。特别是我会警告不要使用类的已弃用方法和构造函数Date
,例如Date(String)
您使用的构造函数。它们已被弃用,因为它们不能跨时区可靠地工作,所以不要使用它们。是的,该类的大多数构造函数和方法都已弃用。
While at the time you asked the question, Joda-Time was (from all I know) a clearly better alternative, time has moved on again. Today Joda-Time is a largely finished project, and its developers recommend you use java.time
, the modern Java date and time API, instead. I will show you how.
虽然在你问这个问题的时候,Joda-Time 是(据我所知)一个明显更好的选择,但时间又开始了。今天,Joda-Time 是一个基本完成的项目,它的开发人员建议您改用java.time
现代 Java 日期和时间 API。我会告诉你如何。
ZonedDateTime localTime = ZonedDateTime.now(ZoneId.systemDefault());
// Convert Local Time to UTC
OffsetDateTime gmtTime
= localTime.toOffsetDateTime().withOffsetSameInstant(ZoneOffset.UTC);
System.out.println("Local:" + localTime.toString()
+ " --> UTC time:" + gmtTime.toString());
// Reverse Convert UTC Time to Local time
localTime = gmtTime.atZoneSameInstant(ZoneId.systemDefault());
System.out.println("Local Time " + localTime.toString());
For starters, note that not only is the code only half as long as yours, it is also clearer to read.
首先,请注意,代码不仅只有你的一半,而且阅读起来也更清晰。
On my computer the code prints:
在我的电脑上,代码打印:
Local:2017-09-02T07:25:46.211+02:00[Europe/Berlin] --> UTC time:2017-09-02T05:25:46.211Z
Local Time 2017-09-02T07:25:46.211+02:00[Europe/Berlin]
I left out the milliseconds from the epoch. You can always get them from System.currentTimeMillis();
as in your question, and they are independent of time zone, so I didn't find them intersting here.
我省略了纪元的毫秒数。你总是可以从System.currentTimeMillis();
你的问题中得到它们,而且它们与时区无关,所以我没有发现它们在这里很有趣。
I hesitatingly kept your variable name localTime
. I think it's a good name. The modern API has a class called LocalTime
, so using that name, only not capitalized, for an object that hasn't got type LocalTime
might confuse some (a LocalTime
doesn't hold time zone information, which we need to keep here to be able to make the right conversion; it also only holds the time-of-day, not the date).
我犹豫着保留了你的变量名localTime
。我认为这是一个好名字。现代 API 有一个名为 的类LocalTime
,因此对于没有类型的对象使用该名称(只是不使用大写)LocalTime
可能会混淆一些(aLocalTime
不包含时区信息,我们需要将其保留在这里以便能够正确的转换;它也只保存时间,而不是日期)。
Your conversion from local time to UTC was incorrect and impossible
您从当地时间到 UTC 的转换不正确且不可能
The outdated Date
class doesn't hold any time zone information (you may say that internally it always uses UTC), so there is no such thing as converting a Date
from one time zone to another. When I just ran your code on my computer, the first line it printed, was:
过时的Date
类不包含任何时区信息(您可能会说它在内部始终使用 UTC),因此没有将 aDate
从一个时区转换为另一个时区的事情。当我刚刚在我的电脑上运行你的代码时,它打印的第一行是:
Local:Sat Sep 02 07:25:45 CEST 2017,1504329945967 --> UTC time:Sat Sep 02 05:25:45 CEST 2017-1504322745000
07:25:45 CEST
is correct, of course. The correct UTC time would have been 05:25:45 UTC
, but it says CEST
again, which is incorrect.
07:25:45 CEST
是正确的,当然。正确的 UTC 时间应该是05:25:45 UTC
,但它又说CEST
,这是不正确的。
Now you will never need the Date
class again, :-) but if you were ever going to, the must-read would be All about java.util.Dateon Jon Skeet's coding blog.
现在您将不再需要该Date
课程,:-) 但如果您打算这样做,则必须阅读Jon Skeet 编码博客上关于 java.util.Date 的全部内容。
Question: Can I use the modern API with my Java version?
问:我可以在我的 Java 版本中使用现代 API 吗?
If using at least Java 6, you can.
如果至少使用 Java 6,则可以。
- In Java 8 and later the new API comes built-in.
- In Java 6 and 7 get the ThreeTen Backport, the backport of the new classes (that's ThreeTen for JSR-310, where the modern API was first defined).
- On Android, use the Android edition of ThreeTen Backport. It's called ThreeTenABP, and I think that there's a wonderful explanation in this question: How to use ThreeTenABP in Android Project.
- 在 Java 8 和更高版本中,新的 API 是内置的。
- 在 Java 6 和 7 中获得ThreeTen Backport,这是新类的向后移植(即首次定义现代 API 的 JSR-310 的 ThreeTen)。
- 在 Android 上,使用 ThreeTen Backport 的 Android 版本。它叫做 ThreeTenABP,我认为这个问题有一个很好的解释:How to use ThreeTenABP in Android Project。
回答by Basil Bourque
tl;dr
tl;博士
Instant.now() // Capture the current moment in UTC.
.atZone( ZoneId.systemDefault() ) // Adjust into the JVM's current default time zone. Same moment, different wall-clock time. Produces a `ZonedDateTime` object.
.toInstant() // Extract a `Instant` (always in UTC) object from the `ZonedDateTime` object.
.atZone( ZoneId.of( "Europe/Paris" ) ) // Adjust the `Instant` into a specific time zone. Renders a `ZonedDateTime` object. Same moment, different wall-clock time.
.toInstant() // And back to UTC again.
java.time
时间
The modern approach uses the java.timeclasses that supplanted the troublesome old legacy date-time classes (Date
, Calendar
, etc.).
现代的方法使用java.time是取代了麻烦的旧的遗留日期时类(类Date
,Calendar
等等)。
Your use of the word "local" contradicts the usage in the java.timeclass. In java.time, "local" means anylocality or alllocalities, but not any one particular locality. The java.timeclasses with names starting with "Local…" all lack any concept of time zone or offset-from-UTC. So they do notrepresent a specific moment, they are nota point on the timeline, whereas your Question is all about moments, points on the timeline viewed through various wall-clock times.
您对“本地”一词的使用与java.time类中的用法相矛盾。在java.time 中,“local”是指任何地点或所有地点,但不是任何一个特定地点。名称以“Local...”开头的java.time类都没有任何时区或 UTC 偏移量的概念。因此,他们并不代表一个特定的时刻,他们是不是在时间轴上的一个点,而你的问题是所有的时刻,在时间轴上的点,通过各种挂钟时间观察。
Get the current system time (local time)
获取当前系统时间(本地时间)
If you want to capture the current moment in UTC, use Instant
. The Instant
class represents a moment on the timeline in UTCwith a resolution of nanoseconds(up to nine (9) digits of a decimal fraction).
如果要以 UTC 格式捕获当前时刻,请使用Instant
. 该Instant
级表示时间轴上的时刻UTC,分辨率为纳秒(最多小数的9个位数)。
Instant instant = Instant.now() ; // Capture the current moment in UTC.
Adjust into a time zone by applying a ZoneId
to get a ZonedDateTime
. Same moment, same point on the timeline, different wall-clock time.
通过应用 aZoneId
来调整时区以获得 a ZonedDateTime
。同一时刻,时间线上的同一点,不同的挂钟时间。
Specify a proper time zone namein the format of continent/region
, such as America/Montreal
, Africa/Casablanca
, or Pacific/Auckland
. Never use the 3-4 letter abbreviation such as EST
or IST
as they are nottrue time zones, not standardized, and not even unique(!).
以、、 或等格式指定正确的时区名称。永远不要使用 3-4 个字母的缩写,例如或因为它们不是真正的时区,不是标准化的,甚至不是唯一的(!)。continent/region
America/Montreal
Africa/Casablanca
Pacific/Auckland
EST
IST
ZoneId z = ZoneId.of( "America/Montreal" ) ;
ZonedDateTime zdt = instant.atZone( z ) ; // Same moment, different wall-clock time.
As a shortcut, you can skip the usage of Instant
to get a ZonedDateTime
.
作为一种快捷方式,您可以跳过 的用法Instant
来获取ZonedDateTime
.
ZoneId z = ZoneId.of( "America/Montreal" ) ;
ZonedDateTime zdt = ZonedDateTime.now( z ) ;
Convert Local time to UTC // Works Fine Till here
将本地时间转换为 UTC // 工作正常,直到这里
You can adjust from the zoned date-time to UTC by extracting an Instant
from a ZonedDateTime
.
您可以通过提取自划的日期-时间调整为UTCInstant
从ZonedDateTime
。
ZoneId z = ZoneId.of( "America/Montreal" ) ;
ZonedDateTime zdt = ZonedDateTime.now( z ) ;
Instant instant = zdt.toInstant() ;
Reverse the UTC time, back to local time.
反转UTC时间,回到当地时间。
As shown above, apply a ZoneId
to adjust the same moment into another wall-clock time used by the people of a certain region (a time zone).
如上图,应用 aZoneId
将同一时刻调整为某个地区(某个时区)的人们使用的另一个挂钟时间。
Instant instant = Instant.now() ; // Capture current moment in UTC.
ZoneId zDefault = ZoneId.systemDefault() ; // The JVM's current default time zone.
ZonedDateTime zdtDefault = instant.atZone( zDefault ) ;
ZoneId zTunis = ZoneId.of( "Africa/Tunis" ) ; // The JVM's current default time zone.
ZonedDateTime zdtTunis = instant.atZone( zTunis ) ;
ZoneId zAuckland = ZoneId.of( "Pacific/Auckland" ) ; // The JVM's current default time zone.
ZonedDateTime zdtAuckland = instant.atZone( zAuckland ) ;
Going back to UTC from a zoned date-time, call ZonedDateTime::toInstant
. Think of it conceptually as: ZonedDateTime = Instant + ZoneId.
从分区日期时间返回 UTC,调用ZonedDateTime::toInstant
. 从概念上将其视为:ZonedDateTime = Instant + ZoneId。
Instant instant = zdtAuckland.toInstant() ;
All of these objects, the Instant
and the three ZonedDateTime
objects all represent the very same simultaneous moment, the same point in history.
所有这些物体,这Instant
三个ZonedDateTime
物体都代表了同一时刻,历史上的同一点。
Followed 3 different approaches (listed below) but all the 3 approaches retains the time in UTC only.
遵循 3 种不同的方法(如下所列),但所有 3 种方法都仅保留 UTC 时间。
Forget about trying to fix code using those awful Date
, Calendar
, and GregorianCalendar
classes. They are a wretched mess of bad design and flaws. You need never touch them again. If you must interface with old code not yet updated to java.time, you can convert back-and-forth via new conversion methods added to the old classes.
忘记使用这些可怕试图修复代码Date
,Calendar
和GregorianCalendar
类别。它们是糟糕的设计和缺陷的一团糟。你再也不需要碰它们了。如果您必须与尚未更新为java.time 的旧代码交互,您可以通过添加到旧类的新转换方法来回转换。
About java.time
关于java.time
The java.timeframework is built into Java 8 and later. These classes supplant the troublesome old legacydate-time classes such as java.util.Date
, Calendar
, & SimpleDateFormat
.
该java.time框架是建立在Java 8和更高版本。这些类取代了麻烦的旧的遗留日期时间类,例如java.util.Date
, Calendar
, & SimpleDateFormat
。
The Joda-Timeproject, now in maintenance mode, advises migration to the java.timeclasses.
现在处于维护模式的Joda-Time项目建议迁移到java.time类。
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
要了解更多信息,请参阅Oracle 教程。并在 Stack Overflow 上搜索许多示例和解释。规范是JSR 310。
You may exchange java.timeobjects directly with your database. Use a JDBC drivercompliant with JDBC 4.2or later. No need for strings, no need for java.sql.*
classes.
您可以直接与您的数据库交换java.time对象。使用符合JDBC 4.2或更高版本的JDBC 驱动程序。不需要字符串,不需要类。java.sql.*
Where to obtain the java.time classes?
从哪里获得 java.time 类?
- Java SE 8, Java SE 9, Java SE 10, and later
- Built-in.
- Part of the standard Java API with a bundled implementation.
- Java 9 adds some minor features and fixes.
- Java SE 6and Java SE 7
- Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
- Android
- Later versions of Android bundle implementations of the java.time classes.
- For earlier Android (<26), the ThreeTenABPproject adapts ThreeTen-Backport(mentioned above). See How to use ThreeTenABP….
- Java SE 8、Java SE 9、Java SE 10及更高版本
- 内置。
- 具有捆绑实现的标准 Java API 的一部分。
- Java 9 添加了一些小功能和修复。
- Java SE 6和Java SE 7
- 多的java.time功能后移植到Java 6和7在ThreeTen-反向移植。
- 安卓
- java.time 类的更高版本的 Android 捆绑实现。
- 对于早期的 Android(<26),ThreeTenABP项目采用了ThreeTen-Backport(上面提到过)。请参阅如何使用ThreeTenABP ...。
The ThreeTen-Extraproject extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval
, YearWeek
, YearQuarter
, and more.
该ThreeTen-额外项目与其他类扩展java.time。该项目是未来可能添加到 java.time 的试验场。你可能在这里找到一些有用的类,比如Interval
,YearWeek
,YearQuarter
,和更多。