dimanche 11 juillet 2021

Date manipulation

 LocalDateTime localDateTime = LocalDateTime.now();

int year = localDateTime.getYear();

int month = localDateTime.getMonthValue();

int day = localDateTime.getDayOfMonth();

int hour = localDateTime.getHour();

int minute = localDateTime.getMinute();

int seconde = localDateTime.getSecond();


System.out.println("local date:" + year + "/" + month + "/" + day + ":" + hour + ":" + minute + ":" + seconde);

local date:2021/7/12:12:18:18

-------------------------------------------------------------------------------------

int year = service.getDateService().getYear()+1900;

int month = service.getDateService().getMonth()+1;

System.out.println("année service:"+year);

System.out.println("mois service:"+month);

System.out.println("mois service:"+String.valueOf(year)+String.format("%02d", month));

//année service:2021

//mois service:7

//mois service:202107

   

--> how to resolve a deprecated getYear() and getMonth():

//Initialize your Date however you like it.

Date date = service.getDateService();

Calendar calendar = new GregorianCalendar();

calendar.setTime(date);

int annee = calendar.get(Calendar.YEAR);

//Add one to month {0 - 11}

int mois = calendar.get(Calendar.MONTH) + 1;

int jours = calendar.get(Calendar.DAY_OF_MONTH);

System.out.println("année:"+annee);

System.out.println("mois:"+mois);

System.out.println("jours:"+jours);

//année:2021

//mois:7

//jours:10

Aucun commentaire:

Enregistrer un commentaire