Java API Time - Period - OCA JSE 8

package com.codemonkey; import static java.lang.System.out; import static java.lang.System.err; import java.time.Duration; import java.time.Instant; import java.time.LocalDate; import java.time.Month; import java.time.Period; import java.time.temporal.TemporalAmount; import java.time.DateTimeException; public class Main{ static public void main(String $[]){ getPeriodDemo(); } public static void getPeriodDemo(){ CharSequence charSequence = "P1D"; Period period = Period.parse(charSequence); out.println(period);//P1D int years = 2; int months = 4; int days = 120; period = Period.of(years, months, days); out.println(period);//P2Y4M120D out.println(period.getYears());//2 out.println(period.getMonths());//4 out.println(period.getDays());//120 years = 3; period = Period.ofYears(years); out.println(period);//P3Y months = 6; period = Period.ofMonths(months); out.println(period);//P6M days = 15; period = Period.ofDays(days); out.println(period);//P15D int weeks = 4;// 1 semana = 7 dias; 4 x 7 = 28 dias period = Period.ofWeeks(weeks); out.println(period);//P28D TemporalAmount temporalAmount = Period.from(Period.ofMonths(12));//P12M period = Period.from(temporalAmount); out.println(period);//P12M temporalAmount = Period.from(Period.ofYears(4));//P4Y period = Period.from(temporalAmount); out.println(period);//P4Y temporalAmount = Period.from(Period.ofDays(120));//P120D period = Period.from(temporalAmount); out.println(period);//P120D //1 semana = 7 dias ; 30 x 7 = 210 dias temporalAmount = Period.from(Period.ofWeeks(30));//P210D period = Period.from(temporalAmount); out.println(period);//P210D period = Period.parse("P1Y2M3D").plusYears(1).plusMonths(2).plusDays(3); out.println(period);//P2Y4M6D period = period.minusYears(1).minusMonths(2).minusDays(3); out.println(period);//P1Y2M3D Month mesA = Month.JULY; Month mesB = Month.DECEMBER; LocalDate localDateA = LocalDate.of(2010, mesA, 12); LocalDate localDateB = LocalDate.of(2010, mesB, 12); period = Period.between(localDateA, localDateB); out.println(period);//P5M mesA = Month.JULY; mesB = Month.MARCH; localDateA = LocalDate.of(2010, mesA, 12); localDateB = LocalDate.of(2010, mesB, 12); period = Period.between(localDateA, localDateB); out.println(period);//P-4M LocalDate today = LocalDate.now(); LocalDate birthday = LocalDate.of(1981, Month.DECEMBER, 18); LocalDate nextBDay = birthday.withYear(today.getYear()); period = Period.between(today, nextBDay); out.println(period);//P7M11D Instant t1 = Instant.now(); out.println(t1);//2018-05-07T14:14:00.834Z Instant t2 = Instant.now(); out.println(t2);//2018-05-07T14:14:00.852Z long ns = Duration.between(t1, t2).toNanos(); out.println(ns);//18000000 try{ Period periodo = Period.parse("2010-02-12"); out.println(periodo); }catch(DateTimeException | IllegalArgumentException e){ err.println("Excepcion en formato de periodo:"); e.printStackTrace(); } //Salida: //Excepcion en formato de periodo: //java.time.format.DateTimeParseException: Text cannot be parsed to a Period //at java.time.Period.parse(Period.java:350) //... LocalDate oldDate = LocalDate.of(1981, Month.DECEMBER, 18); LocalDate newDate = LocalDate.now(); //LocalDate.of(2018, Month.MAY, 7); out.println(oldDate);//1981-12-18 out.println(newDate);//2018-05-07 period = Period.between(oldDate, newDate); out.print(period.getYears() + " años,"); out.print(period.getMonths() + " meses,"); out.print(period.getDays() + " dias\n");//36 años,4 meses,19 dias period = Period.ZERO; out.println(period);//P0D } }
Ejemplos de java.time.Period

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.