- Gregorian Calendar
- GMT and UTC
- GMT
- GMT is a time zone officially used in some European and African countries.
- UTC
- UTC is not a time zone, but a time standard that is the basis for civil time and time zones worldwide. This means that no country or territory officially use UTC as a local time.
- GMT
- UTC, GMT and Daylight Saving Time
- Neither UTC nor GMT ever change for Daylight Saving Time(DST). However, some of the contries that use GMT switch different time zones during their DST period.
- For example, the United Kingdom is not on GMT all year, it uses British Summer Time (BST), which is one hour ahead of GMT, during the summer months.
- Day Light Saving Time(DST)
- History: http://www.timeanddate.com/time/dst/history.html
- DST is used to save energy and make better use of daylight
- Clocks are set ahead one hour when DST starts. This means that the sunrise and sunset will be one hour later, on the clock, than the day before
- Date Time in Java
- standard before JDK1.8: java.util.Date represents a specific moment in time
- Joda Time
- The primary class used to represent an instant in time is the org.joda.time.DateTime. A DateTime, as it name implies, encodes both the date and the time. It also includes time zone information so that it knows how to interpret it in terms of hours and minutes. This object is immutable, which means it is thread-safe. It also means that when you perform "date math" on that object, you get a brand new DateTime as a result.
- Joda Time lets you use the intuitive numbers for months, with 1 meaning January and 12 December
- Joda Time has another class you may want to use to represent a particular date and time, LocalDateTime. It is very similar to the DateTime, except that it does not include any time zone information
- Durations, Periods, Intervals
- Duration: A duration is a length of time, which Joda Time encodes as the number of milliseconds. Joda Time uses periods to do the date math on DateTime. If you specify a Period or Duration using DateTimes that cross a DST change, or from different time zones, it does "the right thing"
-
DateTime beforeDST = new DateTime(2015, 3, 10, 1, 45); DateTime afterDST = new DateTime(2015, 3, 10, 3, 45); Period period = new Period(beforeDST, afterDST); System.out.println("#Hours between 1:45 and 3:45: " + period.getHours()); // #Hours between 1:45 and 3:45: 1
- Interval: represents a specific span of mathematical intervals, closed at the start(inclusive) end at endpoint(exclusive).
- Durations, Periods and Intervals are immutable(thread safe). Use the with*() method to get the modified values
- When you store a DateTime, use a consistent time zone. In many cases, your best option is using UTC because it’s free of DST silliness
- This blog content mostly comes from Nancy Deschenes‘s Date and Time Manipulation in Java Using Joda Time
- end
时间: 2024-10-22 10:27:48