Java API Time 8

import static java.lang.System.out; import java.time.LocalDate; import java.time.Month; import java.time.Clock; import java.time.DayOfWeek; import java.time.ZoneId; import java.time.temporal.ChronoUnit; public class Main{ public static void main(String[] args){ //getLocalDateDemo(); CharSequence charSequence = "2011-12-18"; DayOfWeek dayOfWeek = LocalDate.parse(charSequence).getDayOfWeek(); out.println(dayOfWeek +" : "+diaDeFecha(dayOfWeek));//SUNDAY : DOMINGO int dayOfMonth = LocalDate.parse(charSequence).getDayOfMonth(); out.println(dayOfMonth);//18 int dayOfYear = LocalDate.parse(charSequence).getDayOfYear(); out.println(dayOfYear);//352 Month month = LocalDate.parse(charSequence).getMonth(); out.println(month);//DECEMBER int monthValue = LocalDate.parse(charSequence).getMonthValue(); int yearValue = LocalDate.parse(charSequence).getYear() ; out.println(monthValue);//12 out.println(yearValue);//2011 Month mes = Month.AUGUST; LocalDate localDate = LocalDate.of(2009,mes,9); out.println(localDate);//2009-08-09 out.println(); localDate = LocalDate.of(2010,10,24); out.println(localDate);//2010-10-24 localDate = LocalDate.of(2011,Month.DECEMBER,18); out.println(localDate);//2011-11-18 out.println(); int year = 2006; localDate = LocalDate.ofYearDay(year, 30); out.println(localDate);//2006-01-30 String bisiesto = localDate.isLeapYear()? "Es año bisiesto" : "No es año bisiesto"; out.println(bisiesto); int menos = 1; //2006 - 1 = 2005 localDate = LocalDate.ofYearDay(year, 30).minusYears(menos); out.println(localDate);//2005-01-30 bisiesto = localDate.isLeapYear()? "Es año bisiesto" : "No es año bisiesto"; out.println(bisiesto); boolean before = LocalDate.parse("2019-05-01").isBefore(LocalDate.parse("2019-04-01")); String antes = before? "Si es antes" : "No, no es antes"; out.println(antes); } public static String diaDeFecha(DayOfWeek dayOfWeek){ String dia = ""; String dato = dayOfWeek.toString(); switch(dato){ case "FRIDAY": dia = "VIERNES"; break; case "MONDAY": dia = "LUNES"; break; case "SATURDAY": dia = "SABADO"; break; case "SUNDAY": dia = "DOMINGO"; break; default: dia= "No especificado" ; break; case "THURSDAY": dia = "JUEVES"; break; case "TUESDAY": dia = "MARTES"; break; case "WEDNESDAY": dia = "MIERCOLES"; break; } return dia; } public static void getLocalDateDemo() { LocalDate localDateA = LocalDate.now(); out.println(localDateA);//tambien puede ser: localDateA.toString() //2018-05-05 Clock clock = Clock.systemDefaultZone(); LocalDate localDateB = LocalDate.now(clock); out.println(localDateB);//2018-05-05 ZoneId zoneId = ZoneId.of("Mexico/General"); LocalDate localDateC = LocalDate.now(zoneId); out.println(localDateC);//2018-05-05 out.println("**************"); LocalDate localDateD = LocalDate.of(2017,05,05); out.println(localDateD);//2017-05-05 CharSequence charSequence = "2017-05-02"; LocalDate localDateE = LocalDate.parse(charSequence); out.println(localDateE);//2017-05-02 LocalDate localDateF = localDateE.plusDays(3); out.println(localDateF);//2017-05-05 LocalDate localDateG = localDateF.minus(3, ChronoUnit.DAYS); out.println(localDateG);//2017-05-02 out.println("**************"); } }
Ejemplos usando la clase java.time.LocalDate

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.