package com.codemonkey;
import static java.lang.System.out;
import static java.lang.System.err;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.Month;
import java.time.OffsetDateTime;
import java.time.Duration;
import java.time.LocalDate;
import java.time.Period;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.UnsupportedTemporalTypeException;
import java.util.ArrayList;
public class Main{
static public void main(String $[]){
getTimeAPIDemo();
}
static void getTimeAPIDemo(){
LocalDate localDate = LocalDate.now();
out.println(localDate);//2018-05-07
Period period = Period.parse("P2Y");
out.println(period.getMonths());//0
try{
out.println(LocalDateTime.now().format(java.time.format.DateTimeFormatter.ISO_DATE));
out.println(LocalDateTime.now().format(java.time.format.DateTimeFormatter.ISO_DATE_TIME));
out.println(LocalDateTime.now().format(java.time.format.DateTimeFormatter.ISO_INSTANT));
out.println(LocalDateTime.now().format(java.time.format.DateTimeFormatter.ISO_LOCAL_DATE));
out.println(LocalDateTime.now().format(java.time.format.DateTimeFormatter.ISO_LOCAL_DATE_TIME));
out.println(LocalDateTime.now().format(java.time.format.DateTimeFormatter.ISO_OFFSET_DATE));
out.println(LocalDateTime.now().format(java.time.format.DateTimeFormatter.ISO_OFFSET_DATE_TIME));
out.println(LocalDateTime.now().format(java.time.format.DateTimeFormatter.ISO_OFFSET_TIME));
out.println(LocalDateTime.now().format(java.time.format.DateTimeFormatter.ISO_ORDINAL_DATE));
out.println(LocalDateTime.now().format(java.time.format.DateTimeFormatter.ISO_TIME));
out.println(LocalDateTime.now().format(java.time.format.DateTimeFormatter.ISO_WEEK_DATE));
out.println(LocalDateTime.now().format(java.time.format.DateTimeFormatter.ISO_ZONED_DATE_TIME));
out.println(LocalDateTime.now().format(java.time.format.DateTimeFormatter.BASIC_ISO_DATE));
}catch(UnsupportedTemporalTypeException ute){
err.println("Excepcion: campo no soportado");
ute.printStackTrace();
}
localDate = LocalDate.of(1940, Month.JANUARY, 3);
out.println(localDate);//1940-01-03
localDate = localDate.minusYears(3).plusMonths(10).plusDays(5);
out.println(localDate);//1937-11-08
localDate.minusYears(10);//aqui no pasa nada
out.println(localDate);//1937-11-08
out.println(localDate.getMonth()
+" - "+localDate.getYear());//NOVEMBER - 1937
String date1 = "2011-12-03";
DateTimeFormatter formatter1 = DateTimeFormatter.ofPattern("yyyy-MM-dd");
localDate = LocalDate.parse(date1, formatter1);
out.println(localDate.toString());//2011-12-03
//String date2 = "2014-12-03 00:00:00";//Esto no es correcto
String date2 = "2014-12-03T00:00:00";//Esto es correcto
DateTimeFormatter formatter2 = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
localDate = LocalDate.parse(date2, formatter2);
out.println(localDate.toString());//2014-12-03
localDate = LocalDate.of(2019, Month.APRIL, 30);
out.println(localDate.toString());//2019-04-30
period = Period.parse("P2Y8M");
out.println(period.getYears());//2
out.println(period.getMonths());//8
out.println(period.getDays());//0
out.println(period.toTotalMonths());//32
Duration duration = Duration.ofSeconds(355);
out.println(duration.getSeconds());//355
duration = Duration.ofNanos(90909090);
out.println(duration.getNano());//90909090
out.println(duration.getSeconds());//0
LocalDateTime localDateTime = LocalDateTime.now().withYear(2019);
out.println(localDateTime);//2019-05-07T15:36:36.340
localDateTime = LocalDateTime.now().withMonth(11);
out.println(localDateTime);//2018-11-07T15:37:26.823
localDateTime = LocalDateTime.now().withHour(10);
out.println(localDateTime);//2018-05-07T10:38:05.398
localDateTime = LocalDateTime.now().withMinute(45);
out.println(localDateTime);//2018-05-07T15:45:45.689
localDateTime = LocalDateTime.now().withSecond(32);
out.println(localDateTime);//2018-05-07T15:40:32.238
period = Period.of(3,0,1);//of(año, mes, dia)
out.println(period.toString());//P3Y1D
period = Period.of(0,2,1);
out.println(period.toString());//P2M1D
period = Period.of(3,2,0);
out.println(period.toString());//P3Y2M
//Esto no se puede:
//period = Period.of(0,Month.DECEMBER,0);
period = Period.of(0,12,0);
out.println(period.toString());//P12M
period = Period.of(2017, 3, 1);
period = period.plusDays(1);
out.println(period.toString());//P2017Y3M2D
period = Period.of(2015, 3, 1);
period = period.plusYears(5);
out.println(period.toString());//P2020Y3M1D
period = Period.of(2018, 3, 1);
period = period.plusMonths(3);
out.println(period.toString());//P2018Y6M1D
period = Period.of(2018, 3, 1);
period = period.minusYears(3);
out.println(period.toString());//P2015Y3M1D
period = Period.of(2018, 3, 1);
period = period.minusMonths(2);
out.println(period.toString());//P2018Y1M1D
period = Period.of(2018, 3, 1);
period = period.minusDays(1);
out.println(period.toString());//P2018Y3M
LocalTime localTime = LocalTime.MAX;
out.println(localTime.toSecondOfDay());//86399
LocalTime localTimeTemp = LocalTime.MIDNIGHT;
localDateTime = localTimeTemp.atDate(LocalDate.now());
out.println(localDateTime);//2018-05-07T00:00
period = Period.of(3,4,7);
int anyos = period.getYears();
int meses = period.getMonths();
int dias = period.getDays();
out.print(anyos+" años, "+meses+" meses,"+dias+" dias\n");
localDate = LocalDate.now();
out.println(localDate
.format(DateTimeFormatter.ISO_WEEK_DATE));//2018-W19-1
OffsetDateTime odt = OffsetDateTime.now();
out.println(odt);//2018-05-07T16:54:53.905-05:00
out.println(odt
.format(DateTimeFormatter.ISO_DATE));//2018-05-07-05:00
out.println(odt
.format(DateTimeFormatter.ISO_OFFSET_DATE));//2018-05-07-05:00
out.println(odt
.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME));//2018-05-07T16:56:43.751-05:00
out.println("----------------------------------------------");
ZonedDateTime zdt = ZonedDateTime.now();
out.println(zdt);//2018-05-07T16:58:19.733-05:00[America/Mexico_City]
out.println(zdt
.format(DateTimeFormatter.ISO_DATE));//2018-05-07-05:00
out.println(zdt
.format(DateTimeFormatter.ISO_OFFSET_DATE));//2018-05-07-05:00
out.println(zdt
.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME));//2018-05-07T16:58:19.733-05:00
out.println(zdt
.format(DateTimeFormatter.ISO_ZONED_DATE_TIME));//2018-05-07T17:00:01.615-05:00[America/Mexico_City]
out.println("**************Lista*************");
ArrayList<DateTimeFormatter> ldtFormatterList = new ArrayList<>();
ldtFormatterList.add(0, DateTimeFormatter.BASIC_ISO_DATE);//20180507
ldtFormatterList.add(1, DateTimeFormatter.ISO_LOCAL_TIME);//17:06:13.465
ldtFormatterList.add(2, DateTimeFormatter.ISO_LOCAL_DATE);//2018-05-07
ldtFormatterList.add(3, DateTimeFormatter.ISO_DATE);//2018-05-07
ldtFormatterList.add(4, DateTimeFormatter.ISO_TIME);//17:06:13.465
ldtFormatterList.add(5, DateTimeFormatter.ISO_DATE_TIME);//2018-05-07T17:06:13.465
ldtFormatterList.add(6, DateTimeFormatter.ISO_ORDINAL_DATE);//2018-127
if(!ldtFormatterList.isEmpty()){
LocalDateTime ldt = LocalDateTime.now();
ldtFormatterList.forEach(c -> {
out.println(ldt.format(c));
});
//ldtFormatterList.forEach(out::println);
}
}
}
Java API Time - OCA JSE 8 Ejemplos
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.