Java Year Month getYearAndMonth()

Here you can find the source of getYearAndMonth()

Description

get Year And Month

License

Apache License

Declaration

public static String getYearAndMonth() 

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 final Object lock = new Object();
    private static Map<String, ThreadLocal<SimpleDateFormat>> localThreadMap = new HashMap<String, ThreadLocal<SimpleDateFormat>>();
    public final static String PATTERN_YYYYMM = "yyyy-MM";

    public static String getYearAndMonth() {
        return toString(new Date(), PATTERN_YYYYMM);
    }//from ww w  .  j a v a  2  s. c o  m

    public static String toString(Date source, String formater) {
        SimpleDateFormat format = getDateFormat(formater);
        return format.format(source);
    }

    public static SimpleDateFormat getDateFormat(final String pattern) {
        ThreadLocal<SimpleDateFormat> threadLoacl = localThreadMap.get(pattern);
        if (threadLoacl == null) {
            synchronized (lock) {
                threadLoacl = localThreadMap.get(pattern);
                if (threadLoacl == null) {
                    threadLoacl = new ThreadLocal<SimpleDateFormat>() {
                        @Override
                        protected SimpleDateFormat initialValue() {
                            return new SimpleDateFormat(pattern);
                        }
                    };
                    localThreadMap.put(pattern, threadLoacl);
                }
            }
        }
        return threadLoacl.get();
    }
}

Related

  1. getStartMonth(int year, int month)
  2. getTheLastDayOfTheMonth(int year, int month)
  3. getThisYearMonth()
  4. getThisYearMonth()
  5. getTimeByYearMonth(String yearMonth, String timeZone)
  6. getYearMonth()
  7. getYearMonth()
  8. getYearMonth(Calendar sDate)
  9. getYearMonth(Date date)