Java Month Day getLastMothStart()

Here you can find the source of getLastMothStart()

Description

get Last Moth Start

License

Open Source License

Declaration

public static String getLastMothStart() 

Method Source Code

//package com.java2s;

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

public class Main {
    public static String getLastMothStart() {

        Calendar c = getCurCalendar();
        c.add(Calendar.MONTH, -1);
        c.set(Calendar.DAY_OF_MONTH, 1);

        return formartRandomDate(c.getTime(), "yyyy-MM-dd");
    }//from w w  w . j  av a 2s. co  m

    public static Calendar getCurCalendar() {
        return Calendar.getInstance();
    }

    public static String formartRandomDate(Date date, String strFormat) {
        if (date == null || strFormat == null) {
            return "";
        }

        String key = strFormat;
        key = key.replaceAll("(?<!\\\\)yyyy", getDate(date, "yyyy"));
        key = key.replaceAll("(?<!\\\\)yy", getDate(date, "yy"));
        key = key.replaceAll("\\\\y", "y");

        key = key.replaceAll("(?<!\\\\)MM", getDate(date, "MM"));
        key = key.replaceAll("(?<!\\\\)M", getDate(date, "M"));
        key = key.replaceAll("(?<!\\\\)mm", getDate(date, "mm"));
        key = key.replaceAll("(?<!\\\\)m", getDate(date, "m"));
        key = key.replaceAll("\\\\(M|m)", "$1");

        key = key.replaceAll("(?<!\\\\)dd", getDate(date, "dd"));
        key = key.replaceAll("(?<!\\\\)d", getDate(date, "d"));
        key = key.replaceAll("\\\\d", "d");

        key = key.replaceAll("(?<!\\\\)hh", getDate(date, "hh"));
        key = key.replaceAll("(?<!\\\\)h", getDate(date, "h"));
        key = key.replaceAll("(?<!\\\\)HH", getDate(date, "HH"));
        key = key.replaceAll("(?<!\\\\)H", getDate(date, "H"));
        key = key.replaceAll("\\\\(H|h)", "$1");

        key = key.replaceAll("(?<!\\\\)ss", getDate(date, "ss"));
        key = key.replaceAll("(?<!\\\\)s", getDate(date, "s"));
        key = key.replaceAll("(?<!\\\\)SSS", getDate(date, "SSS"));
        key = key.replaceAll("(?<!\\\\)SS", getDate(date, "SS"));
        key = key.replaceAll("(?<!\\\\)s", getDate(date, "S"));
        key = key.replaceAll("\\\\(S|s)", "$1");

        key = key.replaceAll("(?<!\\\\)F", getDate(date, "F"));
        key = key.replaceAll("\\\\F", "F");
        key = key.replaceAll("(?<!\\\\)E", getDate(date, "E"));
        key = key.replaceAll("\\\\E", "E");
        key = key.replaceAll("(?<!\\\\)a", getDate(date, "a"));
        key = key.replaceAll("\\\\a", "a");

        return key;
    }

    public static String getTime(Calendar c) {
        return getDate(c.getTime(), "HH:mm:ss");
    }

    public static String getDate() {
        return getDate(getCurDate(), "yyyy-MM-dd");
    }

    public static String getDate(Date date, String format) {

        String dtstr = "";
        if (date == null) {
            return dtstr;
        }

        if (format == null || "".equals(format.trim())) {
            format = "yyyy-MM-dd";
        }

        SimpleDateFormat sdf = new SimpleDateFormat(format);
        dtstr = sdf.format(date);
        return (dtstr == null ? "" : dtstr);

    }

    public static String getDate(Date date) {
        return getDate(date, "yyyy-MM-dd");
    }

    public static Date getDate(long time) {

        Calendar c = getCurCalendar();
        c.setTimeInMillis(time);

        return c.getTime();
    }

    public static Date getCurDate() {
        return getCurCalendar().getTime();
    }
}

Related

  1. getFirstDayStrOfMonth(Date date)
  2. getLastMonthDay()
  3. getLastMonthLastDate()
  4. getLastMonthLastDay()
  5. getLastMonthLastDay()
  6. GetLastWorkDayofMonth(String strDateStart)
  7. getMonthDay(int year, int month)
  8. getMonthEndDay(String time)
  9. getMonthEndDay(String yyyy, String mm)