Here you can find the source of formatShortDate(LocalDateTime dateTime)
Parameter | Description |
---|---|
dateTime | LocalDateTime to format. |
public static String formatShortDate(LocalDateTime dateTime)
//package com.java2s; //License from project: Open Source License import java.time.LocalDate; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.time.temporal.ChronoUnit; public class Main { private static final String DAYS = "days"; /**/*from w ww .j a va2 s . c o m*/ * @@author A0139812A * Formats a LocalDateTime to a short date. Excludes the day of week only if * the date is within 2-6 days from now. * * @param dateTime LocalDateTime to format. * @return Formatted shorten day. */ public static String formatShortDate(LocalDateTime dateTime) { if (dateTime == null) { return null; } LocalDate date = dateTime.toLocalDate(); long daysDifference = LocalDate.now().until(date, ChronoUnit.DAYS); String dateFormat; // Don't show dayOfWeek for days d, such that d = {n+2,...,n+6}, where n = date now if (daysDifference >= 2 && daysDifference <= 6) { dateFormat = "dd MMM"; } else { dateFormat = "E dd MMM"; } return date.format(DateTimeFormatter.ofPattern(dateFormat)); } }