https://docs.oracle.com/javase/tutorial/datetime/TOC.html
LocalDate today = LocalDate.now();
LocalDate dateOfBirth = LocalDate.of(2012, Month.MAY, 14);
LocalDate firstBirthday = dateOfBirth.plusYears(1);
LocalTime thisSec;
for (;;) {
thisSec = LocalTime.now();
display(thisSec.getHour(), thisSec.getMinute(), thisSec.getSecond());
}
System.out.printf("now: %s%n", LocalDateTime.now());
System.out.printf("Apr 15, 1994 @ 11:30am: %s%n",
LocalDateTime.of(1994, Month.APRIL, 15, 11, 30));
System.out.printf("now (from Instant): %s%n",
LocalDateTime.ofInstant(Instant.now(), ZoneId.systemDefault()));
System.out.printf("6 months from now: %s%n",
LocalDateTime.now().plusMonths(6));
System.out.printf("6 months ago: %s%n",
LocalDateTime.now().minusMonths(6));
DateTimeFormatter format = DateTimeFormatter.ofPattern("MMM d yyyy hh:mm a");
LocalDateTime leaving = LocalDateTime.of(2013, Month.JULY, 20, 19, 30);
LocalDate date = LocalDate.parse(in, DateTimeFormatter.BASIC_ISO_DATE);
DateTimeFormatter formatter =
DateTimeFormatter.ofPattern("MMM d yyyy");
LocalDate date = LocalDate.parse(input, formatter);
Instant t1, t2;
...
long ns = Duration.between(t1, t2).toNanos();
Instant start;
...
Duration gap = Duration.ofSeconds(10);
Instant later = start.plus(gap);
LocalDate today = LocalDate.now();
LocalDate birthday = LocalDate.of(1960, Month.JANUARY, 1);
Period p = Period.between(birthday, today);
long p2 = ChronoUnit.DAYS.between(birthday, today);
System.out.println("You are " + p.getYears() + " years, " + p.getMonths() +
" months, and " + p.getDays() +
" days old. (" + p2 + " days total)");
The code produces output similar to the following:
You are 53 years, 4 months, and 29 days old. (19508 days total)
Prefix |
Method Type |
Use |
of |
static factory |
Creates an instance where the factory is primarily validating the input parameters, not converting them. |
from |
static factory |
Converts the input parameters to an instance of the target class, which may involve losing information from the input. |
parse |
static factory |
Parses the input string to produce an instance of the target class. |
format |
instance |
Uses the specified formatter to format the values in the temporal object to produce a string. |
get |
instance |
Returns a part of the state of the target object. |
is |
instance |
Queries the state of the target object. |
with |
instance |
Returns a copy of the target object with one element changed; this is the immutable equivalent to a set method on a JavaBean. |
plus |
instance |
Returns a copy of the target object with an amount of time added. |
minus |
instance |
Returns a copy of the target object with an amount of time subtracted. |
to |
instance |
Converts this object to another type. |
at |
instance |
Combines this object with another. |
https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html
All letters 'A' to 'Z' and 'a' to 'z' are reserved as pattern letters. The
following pattern letters are defined:
Symbol Meaning Presentation Examples
------ ------- ------------ -------
G era text AD; Anno Domini; A
u year year 2004; 04
y year-of-era year 2004; 04
D day-of-year number 189
M/L month-of-year number/text 7; 07; Jul; July; J
d day-of-month number 10
Q/q quarter-of-year number/text 3; 03; Q3; 3rd quarter
Y week-based-year year 1996; 96
w week-of-week-based-year number 27
W week-of-month number 4
E day-of-week text Tue; Tuesday; T
e/c localized day-of-week number/text 2; 02; Tue; Tuesday; T
F week-of-month number 3
a am-pm-of-day text PM
h clock-hour-of-am-pm (1-12) number 12
K hour-of-am-pm (0-11) number 0
k clock-hour-of-am-pm (1-24) number 0
H hour-of-day (0-23) number 0
m minute-of-hour number 30
s second-of-minute number 55
S fraction-of-second fraction 978
A milli-of-day number 1234
n nano-of-second number 987654321
N nano-of-day number 1234000000
V time-zone ID zone-id America/Los_Angeles; Z; -08:30
z time-zone name zone-name Pacific Standard Time; PST
O localized zone-offset offset-O GMT+8; GMT+08:00; UTC-08:00;
X zone-offset 'Z' for zero offset-X Z; -08; -0830; -08:30; -083015; -08:30:15;
x zone-offset offset-x +0000; -08; -0830; -08:30; -083015; -08:30:15;
Z zone-offset offset-Z +0000; -0800; -08:00;
p pad next pad modifier 1
' escape for text delimiter
'' single quote literal '
[ optional section start
] optional section end
# reserved for future use
{ reserved for future use
} reserved for future use
No comments:
Post a Comment