Java Time Now getNowFormattedDateTimeStr(String pattern)

Here you can find the source of getNowFormattedDateTimeStr(String pattern)

Description

get Now Formatted Date Time Str

License

Apache License

Declaration

public static String getNowFormattedDateTimeStr(String pattern) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.text.SimpleDateFormat;

import java.util.Date;
import java.util.HashMap;

import java.util.Map;

public class Main {
    private static Map<String, SimpleDateFormat> pattern2FormatMap = new HashMap<>();

    public static String getNowFormattedDateTimeStr(String pattern) {
        return getFormattedDateTimeStr(pattern, new Date());
    }//from w w  w  .  j  a  va  2  s  .  com

    public static String getFormattedDateTimeStr(String pattern, Date date) {
        SimpleDateFormat simpleDateFormat = getDateFormatFromCache(pattern);
        return simpleDateFormat.format(date);
    }

    private static SimpleDateFormat getDateFormatFromCache(String pattern) {
        SimpleDateFormat simpleDateFormat = null;
        if (pattern2FormatMap.containsKey(pattern)) {
            simpleDateFormat = pattern2FormatMap.get(pattern);
        } else {
            simpleDateFormat = new SimpleDateFormat(pattern);
            pattern2FormatMap.put(pattern, simpleDateFormat);
        }
        return simpleDateFormat;
    }
}

Related

  1. getNowDateTimeStrConcise()
  2. getNowDayTimeLong()
  3. getNowDefault()
  4. getNowFileFormat()
  5. getNowFormatLog()
  6. getNowHour()
  7. getNowInBanFormat()
  8. getNowInputDate()
  9. getNowOfDateByFormat(String format)