A, Clock clock
Clock class provides access to the current date and time, the time zone is sensitive Clock, may be used instead System.currentTimeMillis () to get the current of a few microseconds. A particular point in time can also be used to represent the class Instant, Instant classes can also be used to create the old java.util.Date object.
import java.time.Clock;
import java.time.Instant;
import java.util.Date;
public class Test {
public static void main(String[] args) {
Clock clock = Clock.systemDefaultZone();
long millis = clock.millis();
Instant instant = clock.instant();
Date from = Date.from(instant);
System.out.println("millis :" + millis);
System.out.println("from :" + from);
}
}
Two, Timezones time zone
import java.time.ZoneId;
import java.util.Set;
public class Test {
public static void main(String[] args) {
Set<String> availableZoneIds = ZoneId.getAvailableZoneIds();
System.out.println(availableZoneIds);
ZoneId of = ZoneId.of("Asia/Shanghai");
ZoneId of1 = ZoneId.of("Asia/Aden");
System.out.println(of.getRules());
System.out.println(of1.getRules());
}
}
Three, LocalTime local time
1, can create an instance of LocalDate by static factory method of
2, through the same analytical method parse time
3, the operation mode and the like LocalDate
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
import java.time.temporal.ChronoUnit;
import java.util.Locale;
import java.util.Set;
public class Test {
public static void main(String[] args) {
Set<String> availableZoneIds = ZoneId.getAvailableZoneIds();
System.out.println(availableZoneIds);
//获取当前时间
ZoneId zoneId = ZoneId.of("Asia/Shanghai");
LocalTime localTimeOld = LocalTime.now(zoneId);
System.out.println("localTime :"+localTimeOld);
//获取定时时间及时、分、秒
LocalTime localTime = LocalTime.of(12, 25, 55);
int hour = localTime.getHour();
int minute = localTime.getMinute();
int second = localTime.getSecond();
System.out.println("localTime "+localTime+"\nhour :"+hour + "\nminute :" + minute + "second :"+second);
//获取两个时间时间差(时间小的第一个参数结果是负数)
long hoursBetween = ChronoUnit.HOURS.between(localTimeOld, localTime);
long minutesBetween = ChronoUnit.MINUTES.between(localTimeOld, localTime);
System.out.println("hoursBetween :"+hoursBetween);
System.out.println("minutesBetween :"+minutesBetween);
//多种工厂方法简化对象创建,包括字符串的解析
DateTimeFormatter germanFormatter =
DateTimeFormatter
.ofLocalizedTime(FormatStyle.SHORT)
.withLocale(Locale.GERMAN);
LocalTime parse = LocalTime.parse("12:25", germanFormatter);
System.out.println("parse :"+parse);
}
}
Four, LocalDate local date
LocalDate shows an exact date, the value of the object is immutable, and LocalTime consistent with them. Also note that these objects are immutable, a new instance of the operation is always returned.
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.Month;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
import java.time.temporal.ChronoUnit;
import java.util.Locale;
public class Test {
public static void main(String[] args) {
LocalDate today = LocalDate.now();
LocalDate tomorrow = today.plus(1, ChronoUnit.DAYS);
LocalDate yesterday = tomorrow.minusDays(2);
System.out.println(yesterday);
LocalDate independenceDay = LocalDate.of(2014, Month.JULY, 4);
DayOfWeek dayOfWeek = independenceDay.getDayOfWeek();
System.out.println(dayOfWeek);
DateTimeFormatter germanFormatter = DateTimeFormatter
.ofLocalizedDate(FormatStyle.MEDIUM)
.withLocale(Locale.GERMAN);
LocalDate xmas = LocalDate.parse("01.11.2019", germanFormatter);
System.out.println(xmas);
}
}
Five, LocalDateTime local date and time
1, LocalDateTime, and is LocalTime LocalDate fit. It also shows the date and time, but with no time zone information, can be created directly, or through the merger date and time of object construction.
2, LocalDateTime and LocalTime there LocalDate, are immutable. LocalDateTime provides methods to access specific fields.
import java.time.DayOfWeek;
import java.time.LocalDateTime;
import java.time.Month;
import java.time.temporal.ChronoField;
public class Test {
public static void main(String[] args) {
LocalDateTime sylvester = LocalDateTime.of(2019, Month.DECEMBER, 31, 23, 59, 59);
DayOfWeek dayOfWeek = sylvester.getDayOfWeek();
System.out.println(dayOfWeek);
Month month = sylvester.getMonth();
System.out.println(month);
long minuteOfDay = sylvester.getLong(ChronoField.MINUTE_OF_DAY);
System.out.println(minuteOfDay);
}
}
Six, Instant
From the standpoint of a computer, the modeling time is the most natural form a single large integer points on a continuous period of time, the Instant is designed in order to facilitate use of the machine it is that it contains nanoseconds and seconds the configuration of the digital. Instant class also supports static factory method now, it can help you get the time stamp of the current time.
import java.time.Instant;
public class Test {
public static void main(String[] args) {
Instant now = Instant.now();
System.out.println(now.getEpochSecond() + "." + now.getNano());
System.out.println(System.currentTimeMillis());
}
}
Seven general method of class time
- LocalDate, LocalTime, LocalDateTime this indicates the date and time classes Instant time point provides a number of general methods.
Method name | Whether it is a static method | description |
---|---|---|
form | Yes | Create an object instance of the object passed in Temporal |
now | Yes | Temporal create objects based on the system clock |
of | Yes | The object is created by a part of the Temporal instance of an object |
parse | Yes | Temporal objects created by the string instance |
atOffset | no | The Temporal objects and a combination of the time zone offset |
atZone | no | The Temporal objects and a combination of time zone |
format | no | Using a specified format will Temporal object into a string (Instant class does not provide the method) |
get | no | Temporal a read value of a portion of the object |
minus | no | Temporal create a copy of an object, the copy is created by subtracting certain length of time by the current value of the Temporal object |
plus | no | Temporal create a copy of the object, with a certain time by the current value of the Temporal create long objects of the copy |
with | no | Temporal to the target object creates the object as a template for some state modified copy |
Eight format
DateTimeFormatter commonly used to format the date and time, which are predefined constants like BASIC_ISO_DATE ISO_LOCAL_DATE and convenient format.
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class Test {
public static void main(String[] args) {
LocalDateTime now = LocalDateTime.now();
String s = now.format(DateTimeFormatter.BASIC_ISO_DATE);
String s1 = now.format(DateTimeFormatter.ISO_DATE_TIME);
System.out.println(s);
System.out.println(s1);
}
}
Author: Soul Xuan Hao welcome public attention No.
I retain all rights and interests, please indicate the source.
Welcome to the story, the idea of friends and I share, can be sent to e-mail: [email protected]