Java DateTimeFormatter formatByLocale(Date date, Locale locale)

Here you can find the source of formatByLocale(Date date, Locale locale)

Description

format By Locale

License

Creative Commons License

Declaration

public static String formatByLocale(Date date, Locale locale) 

Method Source Code

//package com.java2s;
//License from project: Creative Commons License 

import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
import java.util.Date;
import java.util.Locale;

public class Main {
    public static String formatByLocale(Date date, Locale locale) {
        return formatByLocale(parseToZonedDateTime(date), locale);
    }//from  w  ww  . j a v a 2 s  .c  o  m

    public static String formatByLocale(ZonedDateTime zonedDateTime, Locale locale) {
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM)
                .withLocale(locale);
        return zonedDateTime.format(dateTimeFormatter);
    }

    public static ZonedDateTime parseToZonedDateTime(String dateString, String pattern) {
        return ZonedDateTime.parse(dateString, DateTimeFormatter.ofPattern(pattern));
    }

    public static ZonedDateTime parseToZonedDateTime(Date date) {
        return date.toInstant().atZone(getZoneId());
    }

    private static ZoneId getZoneId() {
        return ZoneId.systemDefault();
    }
}

Related

  1. createFormatterFromPatternString(String formatPattern, Locale locale)
  2. dateTimeFormatterNCForPattern(String pattern)
  3. format(DateTimeFormatter formatter, Object object)
  4. format(long epochMilliSecond)
  5. formatAgo(long timestamp)
  6. formatDate(long date)
  7. formatDate(long milli)
  8. formatDate(long timestamp)
  9. formatDate(long timestamp)