Je me suis lancé dans un projet de suppression de l’utilisation des objets Date et Calendar, au sein des projets Java sur lesquels je travaille. En effet, Java 8 contient maintenant une API avec des objets de date réellement propres (ENFIN), ET threadsafe (ce qui n’était pas le cas jusque là). Donc il n’y a aucune raison pour ne pas l’utiliser.
J’ai fais ci-dessous un petit listing des différences entre l’utilisation des objets Calendar et LocalDateTime/ZonedDateTime.
Création d’un object de base : [sourcecode language=“java”] Calendar calendar = Calendar.getInstance(); format.setTimeZone(TimeZone.getTimeZone(“Europe/Paris”));
ZoneId zoneId = ZoneId.of(“Europe/Paris”);
LocalDateTime localDateTime = LocalDateTime.now(); ZonedDateTime zonedDateTime = ZonedDateTime.of(localDateTime, zoneId); zonedDateTime.get(IsoFields.WEEK_OF_WEEK_BASED_YEAR); [/sourcecode]
Récupération du numéro de la semaine en cours : [sourcecode language=“java”] int nWeeksInYearsCalendar = calendar.get(Calendar.WEEK_OF_YEAR); int nWeeksInYearsZonedDate = zonedDateTime.get(IsoFields.WEEK_OF_WEEK_BASED_YEAR); [/sourcecode]
Placement sur le premier jour de la semaine : [sourcecode language=“java”] calendar.set(Calendar.DAY_OF_WEEK, calendar.getFirstDayOfWeek()); zonedDateTime = zonedDateTime.with(DayOfWeek.MONDAY); [/sourcecode]
Ajout/Suppression de semaine : [sourcecode language=“java”] calendar.add(Calendar.WEEK_OF_YEAR, 1); calendar.add(Calendar.WEEK_OF_YEAR, -2);
zonedDateTime = zonedDateTime.plus(1, ChronoUnit.WEEKS); zonedDateTime = zonedDateTime.plus(-2, ChronoUnit.WEEKS); [/sourcecode]
Transformation en une String de type [sourcecode language=“java”] SimpleDateFormat format = new SimpleDateFormat(“dd/MM/yyyy”, Locale.FRANCE); format.format(calendar.getTime());
zonedDateTime.format(DateTimeFormatter.ofPattern(“dd/MM/yyyy”)); [/sourcecode]
Printing des douze mois en français [sourcecode language=“java”] Calendar calendar = Calendar.getInstance(); SimpleDateFormat format = new SimpleDateFormat(“MMMM”, Locale.FRANCE); for (int n = 0; n < 12; n++) { System.out.println(String.valueOf(-1 * n) + " : " + format.format(calendar.getTime())); calendar.add(Calendar.MONTH, -1); }
LocalDateTime localDateTime = LocalDateTime.now();
LinkedHashMap<String, String> map = new LinkedHashMap<String, String>(); for (int n = 0; n < 12; n++) { System.out.println(String.valueOf(-1 * n) + " : " + localDateTime.format(DateTimeFormatter.ofPattern(“MMMM”, Locale.FRANCE))); localDateTime = localDateTime.plus(-1, ChronoUnit.MONTHS); } [/sourcecode]
De Date à LocalDateTime à Date
[sourcecode language=“java”] Date in = new Date(); LocalDateTime ldt = LocalDateTime.ofInstant(in.toInstant(), ZoneId.systemDefault()); Date out = Date.from(ldt.atZone(ZoneId.systemDefault()).toInstant()); [/sourcecode]
Différence d’heure entre deux LocalDateTime
[sourcecode language=“java”] long diffHours = pastDate.until(LocalDateTime.now(), ChronoUnit.HOURS); long diffHours2 = ChronoUnit.HOURS.between(pastDate, LocalDateTime.now()); [/sourcecode]
LocalDate to date
[sourcecode language=“java”] Date date = Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant()); [/sourcecode]
Les nouveaux commentaires sont désactivés sur cette version statique du blog.