Java LocalDateTime Format formatShortDate(LocalDateTime dateTime)

Here you can find the source of formatShortDate(LocalDateTime dateTime)

Description

format Short Date

License

Open Source License

Parameter

Parameter Description
dateTime LocalDateTime to format.

Return

Formatted shorten day.

Declaration

public static String formatShortDate(LocalDateTime dateTime) 

Method Source Code


//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));
    }
}

Related

  1. formatDateFromTo(LocalDateTime dateFrom, LocalDateTime dateTo)
  2. formatDateGeneric(DateTimeFormatter dateTimeFormatter, LocalDateTime localDateTime)
  3. formatDateTime(LocalDateTime dateTime)
  4. formatDayOfMonth(LocalDateTime dateTime)
  5. formatLocalDateTimeToString(LocalDateTime date)
  6. formattedWeek(LocalDateTime localDateTime)
  7. formatTime(LocalDateTime dateTime)
  8. formatTime(LocalDateTime time)
  9. localDateTime2Str(LocalDateTime localDateTime, String dateTimeFormatter)