JDK8 a été publié et j'ai écrit un petit exemple de la façon d'utiliser la fonction Time DateTime
La copie de code est la suivante:
Package DateTime;
Importer Static Java.Time.Temporal.TemporalAdJusters.LastDayOfMonth;
Importer Static Java.Time.Temporal.TemporalAdJusters.PreviousorSame;
import java.time.dayofweek;
Importer Java.Time.Duration;
import java.time.localdate;
import java.time.localatetime;
import java.time.localtime;
Importer java.time.month;
import java.time.offsettime;
Importer Java.Time.period;
import java.time.zoneid;
import java.time.zoneoffset;
import java.time.zoneddatetime;
import java.time.temporal.chronounit;
Classe publique Datatimes Times {
public static void main (String [] args) {
// Créer un objet temporel
LocalDateTime timePoint = localDateTime.now ();
System.out.println ("- Time actuel ----");
System.out.println (timePoint);
System.out.println ("");
System.out.println ("- Time personnalisé ----");
System.out.println (LocalDate.of (2012, Mois.Decembre, 12));
// valeurs
System.out.println (LocalDate.OfEPOCHDAY (150));
System.out.println (Localtime.of (17, 18));
System.out.println (localtime.parse ("10:15:30"));
System.out.println ("");
System.out.println ("- Obtenez les différentes parties du temps ----");
System.out.println (timePoint.tolocaldate ());
System.out.println (timePoint.getMonth ());
System.out.println (timePoint.getDayOfMonth ());
System.out.println (timePoint.getSecond ());
System.out.println ("");
System.out.println ("--- définir et renvoyer un nouvel objet temporel ---");
LocalDateTime thepast = timePoint.WithDayOfMonth (10) .Withyear (2010);
System.out.println (thepast);
System.out.println ("--- Ajouter 3 semaines ---");
LocalDatetime yetanother = thepast.plusweeks (3) .plus (3,
Chronounit.weeks);
System.out.println (encoreanother);
System.out.println ("");
System.out.println ("--- Utiliser la fonction de réglage du temps ---");
System.out.println (timePoint);
System.out.println (timePoint.With (LastDayOfMonth ()));
System.out.println (timePoint.With (Présentorale (Dayofweek.wednesday)));
System.out.println (timePoint.with (localTime.Now ()));
System.out.println ("");
System.out.println ("--- THILE TRONNÉ BIT EXACT -");
System.out.println (timePoint);
LocalDatetime troncicatedItoMinutes = timePoint
.truncatedTo (chronounit.minutes);
System.out.println (tronquétimetominutes);
LocalDatetime troncicatedTimetOSeconds = timePoint
.truncatedTo (chronounit.seconds);
System.out.println (TruncatedtimeToSeconds);
System.out.println ("");
System.out.println ("--- Utilisez le fuseau horaire ---");
Zoneddatetime zoned_now = zoneddatetime.of (localDateTime.now (),
ZoneId.of ("UTC + 08: 00"));
Système.
.println (zoneed_now.withZonesAmEInstant (ZoneId.of ("UTC + 00: 00")));
System.out.println (zoned_now.getoffset ());
System.out.println ("");
System.out.println ("--- Le décalage du fuseau horaire est utilisé dans le temps ---");
OffSetTime Time = OffsetTime.Now ();
ZONEOFFSET OFFSET = ZONEOFFSET.OF ("+ 02:00");
System.out.println (temps);
System.out.println (Time.WithOffSetsAmyInstant (Offset));
System.out.println ("");
System.out.println ("--- Ajout de temps et soustraction ---");
timePoint = localDateTime.now ();
System.out.println (timePoint);
// 3 ans, 2 mois, 1 jour
Période période 1 = période (3, 2, 1);
System.out.println (timePoint.Plus (période1));
Durée durée = durée. Des secondes (3, 5);
System.out.println (timePoint.Plus (durée));
Durée sixhours = durée.between (
ZoneddateTime.of (localDateTime.Now (), ZoneId.of ("UTC + 08: 00")),
ZonedDateTime.of (localDateTime.Now (), ZoneId.of ("UTC + 02: 00")));
System.out.println (timePoint.Plus (sixHours));
System.out.println ("");
}
}
Résultats en cours d'exécution
La copie de code est la suivante:
- temps de courant ---
2014-03-25T17: 03: 40.553
- temps personnalisé ---
2012-12-12
1970-05-31
17:18
10:15:30
- Obtenez les différentes parties du temps ---
2014-03-25
MARS
25
40
--- Définissez et renvoyez un nouvel objet temporel ---
2010-03-10T17: 03: 40.553
--- Ajouter 3 semaines de plus ---
2010-04-21T17: 03: 40.553
--- Utilisez la fonction de réglage du temps ---
2014-03-25T17: 03: 40.553
2014-03-31T17: 03: 40.553
2014-03-19T17: 03: 40.553
2014-03-25T17: 03: 40.583
--- tronquant le temps exact bit-
2014-03-25T17: 03: 40.553
2014-03-25T17: 03
2014-03-25T17: 03: 40
--- Utilisez le fuseau horaire ---
2014-03-25T09: 03: 40.583Z [UTC]
+08: 00
--- Le décalage du fuseau horaire est utilisé dans le temps ---
17: 03: 40.585 + 08: 00
11: 03: 40.585 + 02: 00
--- Ajouter ou soustraire le temps ---
2014-03-25T17: 03: 40.585
2017-05-26T17: 03: 40.585
2014-03-25T17: 03: 43.585000005
2014-03-25T23: 03: 40.586