Java Month of Year getLastMonthDate(int date)

Here you can find the source of getLastMonthDate(int date)

Description

get Last Month Date

License

Open Source License

Declaration

public static int getLastMonthDate(int date) 

Method Source Code

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

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class Main {
    private static SimpleDateFormat intSDF = new SimpleDateFormat("yyyyMMdd");
    private static final SimpleDateFormat defaultFormater = new SimpleDateFormat("yyyyMMdd");
    private static final SimpleDateFormat customizedFormater = new SimpleDateFormat("yyyyMMdd");

    public static int getLastMonthDate(int date) {
        Calendar calendar = Calendar.getInstance();
        int year = Integer.parseInt(String.valueOf(date).substring(0, 4));
        int month = Integer.parseInt(String.valueOf(date).substring(4, 6));
        int day = Integer.parseInt(String.valueOf(date).substring(6, 8));
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.MONTH, month - 1);
        calendar.set(Calendar.DAY_OF_MONTH, day);
        calendar.add(Calendar.MONTH, -1);
        return Integer.parseInt(intSDF.format(calendar.getTime()));
    }//  ww w  .j ava2s  . c o  m

    public static String format(Date date, String pattern) {
        synchronized (customizedFormater) {
            if (null != pattern && !"".equals(pattern.trim())) {
                customizedFormater.applyPattern(pattern);
            } else {
                throw new IllegalArgumentException("pattern can not be empty");
            }
            return customizedFormater.format(date);
        }
    }

    public static String format(Date date) {
        synchronized (defaultFormater) {
            return defaultFormater.format(date);
        }
    }

    public static String getTime() {
        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
        return formatter.format(currentTime);
    }
}

Related

  1. getLastMonth()
  2. getLastMonth()
  3. getLastMonthAndCycle()
  4. getLastMonthdate()
  5. getLastMonthDate()
  6. getLastMonthOfQuater(Date date)
  7. getLastMonthOfYear()
  8. getMaxDayOfMonth(int year, int month)
  9. getMonth(int week, int year)