Java DateTimeFormatter getFormattedDate()

Here you can find the source of getFormattedDate()

Description

get Formatted Date

License

Open Source License

Declaration

public static String getFormattedDate() 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.time.LocalDate;
import java.time.LocalTime;

public class Main {
    public static final String PATTERN_DEFAULT = "D.M.Y h:m:s";

    public static String getFormattedDate() {
        return formatDate(getCurrentDate(), PATTERN_DEFAULT);
    }//from w w  w.  j  ava  2 s .  com

    public static String formatDate(String unformattedDate, String format) {
        // Format: year-month-day-hours-minutes-seconds
        String[] splitUFDate = unformattedDate.split("-");
        if (splitUFDate.length != 6)
            return "";

        String formattedDate = format;
        String[] patterns = { "Y", "M", "D", "h", "m", "s" };

        int l = Math.min(patterns.length, splitUFDate.length);
        for (int i = 0; i < l; ++i)
            formattedDate = formattedDate.replaceAll(patterns[i], splitUFDate[i]);
        return formattedDate;
    }

    public static String getCurrentDate() {
        LocalDate date = LocalDate.now();
        LocalTime time = LocalTime.now();
        StringBuilder builder = new StringBuilder();
        builder.append(formatNumber(date.getYear(), 4)).append("-");
        builder.append(formatNumber(date.getMonthValue(), 2)).append("-");
        builder.append(formatNumber(date.getDayOfMonth(), 2)).append("-");
        builder.append(formatNumber(time.getHour(), 2)).append("-");
        builder.append(formatNumber(time.getMinute(), 2)).append("-");
        builder.append(formatNumber(time.getSecond(), 2));
        return builder.toString();
    }

    private static String formatNumber(int number, int zeros) {
        String strZeros = repeatString("0", zeros);
        String strNumber = Integer.toString(number);
        return new StringBuilder().append(strZeros.substring(0, zeros - strNumber.length())).append(strNumber)
                .toString();
    }

    private static String repeatString(String string, int times) {
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < times; ++i)
            builder.append(string);
        return builder.toString();
    }
}

Related

  1. getDateFormat(final Locale LOCALE)
  2. getDateFromFormat(String dtStr)
  3. getDateTimeAttributeTimeFormat()
  4. getDateTimeFormatter()
  5. getFormatDate(String format)
  6. getFormattedDateString()
  7. getFullFormatter()
  8. getGermanFormatter()
  9. getIndexGroupingPeriod_slow(String date_format)