import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.OffsetDateTime;
import java.time.ZonedDateTime;
public class Main {
//Obtenez la date et l'heure actuelles
public static void main(String[] args) {
System.out.println(LocalDateTime.now()); //2020-10-31T07:49:16.502455881
System.out.println(OffsetDateTime.now()); //2020-10-31T07:49:16.503291965Z
System.out.println(ZonedDateTime.now()); //020-10-31T07:49:16.504062367Z[Etc/UTC]
System.out.println(LocalDate.now()); //2020-10-31
System.out.println(LocalTime.now()); //07:49:16.504349859
}
}
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.Month;
import java.time.OffsetDateTime;
import java.time.OffsetTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
public class Main {
//Obtenez une date et une heure spécifiques
public static void main(String[] args) {
//Générer la date et l'heure à partir de la date / heure / minute / seconde nanoseconde
var ldt1 = LocalDateTime.of(2019, 1, 10, 10, 20, 30, 513);
var ldt2 = LocalDateTime.of(2019, Month.JANUARY, 10, 10, 20, 30);
//var ldt3 = LocalDateTime.of(2019, 1, 40, 10, 20, 30); //Erreur car elle est hors de portée
System.out.println(ldt1); //2019-01-10T10:20:30.000000513
System.out.println(ldt2); //2019-01-10T10:20:30.000000513
//Générer uniquement la date / heure
var ld = LocalDate.of(2019, 1, 10);
System.out.println(ld); //2019-01-10
var lt = LocalTime.of(10, 20, 30);
System.out.println(lt); //10:20:30
var ldt4 = LocalDateTime.of(ld, lt);
System.out.println(ldt4); //2019-01-10T10:20:30
//Valeur de décalage de fuseau horaire(Objet ZoneOffset)
var odt = OffsetDateTime.of(2019, 1, 10, 10, 20, 30, 999, ZoneOffset.ofHours(9));
System.out.println(odt); //2019-01-10T10:20:30.000000999+09:00
var ot = OffsetTime.of(10, 20, 30, 999, ZoneOffset.ofHours(9));
System.out.println(ot); //10:20:30.000000999+09:00
var zdt = ZonedDateTime.of(2019, 1, 10, 10, 20, 30, 999, ZoneId.of("Asia/Tokyo"));
System.out.println(zdt); //2019-01-10T10:20:30.000000999+09:00[Asia/Tokyo]
}
}
import java.time.LocalDate;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
public class Main {
//Convertir à partir de la chaîne de date / heure
public static void main(String[] args) {
//Date ISO
System.out.println(LocalDate.parse(
"2019-01-01", DateTimeFormatter.ISO_DATE)); //2019-01-01
//Année et jours
System.out.println(LocalDate.parse(
"2019-123", DateTimeFormatter.ISO_ORDINAL_DATE)); //2019-05-03
//Année et nombre de semaines
System.out.println(LocalDate.parse(
"2019-W40-5", DateTimeFormatter.ISO_WEEK_DATE)); //2019-10-04
//Date et heure avec ID de fuseau horaire
System.out.println(ZonedDateTime.parse(
"2019-01-10T10:20:30.000000999+09:00[Asia/Tokyo]",
DateTimeFormatter.ISO_DATE_TIME)); //2019-01-10T10:20:30.000000999+09:00[Asia/Tokyo]
}
}
import java.time.LocalDateTime;
public class Main {
public static void main(String[] args) {
var dt1 = LocalDateTime.of(2019, 12, 31, 10, 20, 30);
var dt2 = LocalDateTime.of(2020, 1, 1, 10, 20, 30);
System.out.println(dt1.equals(dt2)); //false
System.out.println(dt1.isBefore(dt2)); //true
System.out.println(dt1.isAfter(dt2)); //false
}
}
import java.time.LocalDateTime;
import java.time.temporal.ChronoField;
public class Main {
public static void main(String[] args) {
var dt = LocalDateTime.of(2019, 1, 10, 10, 20, 30, 123);
var week = new String[] {"dimanche", "Lundi", "Mardi", "Mercredi","Jeudi", "Vendredi", "samedi"};
System.out.println(dt.getYear() + "An" +dt.getMonthValue() + "Lune" +dt.getDayOfMonth() + "journée" +dt.getDayOfWeek() + " " +dt.getHour() + "Temps" +dt.getMinute() + "Minutes"+ dt.getSecond() + "Secondes"+ dt.getNano() + "ナノSecondes");
System.out.println("Le nom du mois est" + dt.getMonth() +"cette année" + dt.getDayOfYear() + "journée"); //Le nom du mois estJANUARYcette année10journée
//Gagnez du temps avec Chrono Field
System.out.println(dt.get(ChronoField.YEAR) + "An" +dt.get(ChronoField.MONTH_OF_YEAR) + "Lune" +dt.get(ChronoField.DAY_OF_MONTH) + "journée" +week[dt.get(ChronoField.DAY_OF_WEEK) -1] + " "+dt.get(ChronoField.HOUR_OF_DAY) + "Temps" +dt.get(ChronoField.MINUTE_OF_HOUR) + "Minutes" +dt.get(ChronoField.SECOND_OF_MINUTE) + "Secondes" +dt.get(ChronoField.NANO_OF_SECOND) + "ナノSecondes");
}
} //Mercredi 10 janvier 2019 10:20:30 123 nanosecondes
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
public class Main {
public static void main(String[] args) {
var dt1 = LocalDateTime.of(2020, 11, 1, 10, 20, 30);
var dt2 = ZonedDateTime.of(2019, 11, 1, 10, 20, 30, 0, ZoneId.of("Asia/Tokyo"));
System.out.println(dt1.format(DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL))); //Sunday, November 1, 2020
System.out.println(dt2.format(DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG))); //November 1, 2019 at 10:20:30 AM JST
System.out.println(dt1.format(DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM))); //Nov 1, 2020
System.out.println(dt2.format(DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT))); //11/1/19, 10:20 AM
}
}
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
public class Main {
public static void main(String[] args) {
var dt1 = LocalDateTime.of(2020, 11, 1, 10, 20, 30);
var dt2 = ZonedDateTime.of(2020, 11, 1, 10, 20, 30, 0, ZoneId.of("Asia/Tokyo"));
System.out.println(dt1.format(DateTimeFormatter.ofPattern("y.MM.dd H:m:s"))); //2020.11.01 10:20:30
System.out.println(dt2.format(DateTimeFormatter.ofPattern("Y année L mois d jour (E) a K heure m minute s seconde(z)"))); //1 novembre 2020 (dim.) 10:20:30(JST)
}
}
var d = JapaneseDate.of (JapaneseEra.REIWA, 1, 1, 1);
est une erreur//Calendrier japonais
import java.time.chrono.JapaneseDate;
import java.time.chrono.JapaneseEra;
import java.time.format.DateTimeFormatter;
public class Main {
public static void main(String[] args) {
var d = JapaneseDate.of(JapaneseEra.REIWA, 2, 1, 1);
System.out.println(d); //Japanese Reiwa 2-01-01
//G est le problème d'origine
var df = DateTimeFormatter.ofPattern("Gy année MM mois jj jour");
System.out.println(d.format(df)); //Reiwa 01, 2 janvier
}
}
//Nombre de jours écoulés
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.Period;
public class Main {
public static void main(String[] args) {
var dt1 = LocalDateTime.of(2019, 12, 31, 0, 0, 0);
var dt2 = LocalDateTime.of(2020, 11, 1, 10, 20, 30);
var period = Period.between(dt1.toLocalDate(), dt2.toLocalDate());
System.out.println("Différence de date:" +
period.getYears() + "An" + period.getMonths() + "mois" +
period.getDays() + "Journées"); //日付の差:0年10ヶ月1Journées
var duration = Duration.between(dt1, dt2);
System.out.println("Différence de temps:" + duration.toHours() + "temps"); //Différence de temps:7354temps
}
}
//x ans plus tard,il y a y jours
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
public class Main {
public static void main(String[] args) {
var d = LocalDate.of(2020, 11, 1);
System.out.println(d); //2020-11-01
//3 années plus tard
System.out.println(d.plus(3, ChronoUnit.YEARS)); //2023-11-01
//Il y a 22 jours
System.out.println(d.minus(22, ChronoUnit.DAYS)); //2020-10-10
}
}
P2Y2M
= 2 ans et 2 mois-P1DT5M
= il y a 1 jour 5 minutesimport java.time.Duration;
import java.time.LocalDateTime;
import java.time.Period;
public class Main {
public static void main(String[] args) {
var d = LocalDateTime.of(2020, 11, 1, 10, 20, 30);
var period = Period.ofYears(3);
//P<Date>T<temps>
var duration = Duration.parse("P22DT1H1M1S"); //22 jours 1 heure 1 minute 1 seconde
System.out.println(d); //2020-11-01T10:20:30
System.out.println(d.plus(period)); //2023-11-01T10:20:30
System.out.println(d.minus(duration)); //2020-10-10T09:19:29
}
}
//Calendrier à ce jour-Convertir en API Time
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Calendar;
public class Main {
public static void main(String[] args) {
var cal = Calendar.getInstance();
var dt1 = LocalDateTime.ofInstant(cal.toInstant(), ZoneId.systemDefault());
var dt2 = OffsetDateTime.ofInstant(cal.toInstant(), ZoneId.systemDefault());
var dt3 = ZonedDateTime.ofInstant(cal.toInstant(), ZoneId.systemDefault());
System.out.println(dt1); //2020-11-01T03:30:12.156
System.out.println(dt2); //2020-11-01T03:30:12.156Z
System.out.println(dt3); //2020-11-01T03:30:12.156Z[Etc/UTC]
}
}
//Date-Convertir de l'API Time en Calendrier
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.Calendar;
import java.util.Date;
public class Main {
public static void main(String[] args) {
var dt = LocalDateTime.of(2020, 11, 1, 10, 20, 30, 123456789);
var d = Date.from(dt.toInstant(ZoneOffset.of("+09:00")));
//Entrez une valeur entre Date et Calendrier avec la méthode setTime
var cal = Calendar.getInstance();
cal.setTime(d);
System.out.println(cal); //java.util.GregorianCalendar[time=1604193630123,,(Abréviation),,AM_PM=0,HOUR=1,HOUR_OF_DAY=1,MINUTE=20,SECOND=30,MILLISECOND=123,ZONE_OFFSET=0,DST_OFFSET=0]
}
}
Recommended Posts