Java Month of Year getPreviousDateStringByMonth(int months)

Here you can find the source of getPreviousDateStringByMonth(int months)

Description

get Previous Date String By Month

License

Open Source License

Declaration

public static String getPreviousDateStringByMonth(int months) 

Method Source Code


//package com.java2s;
import java.util.Calendar;

import java.text.SimpleDateFormat;

public class Main {

    public static String getPreviousDateStringByMonth(int months) {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH, -months);

        return toDateString(calendar);
    }//from   w  ww .java 2 s.  c o m

    public static String getPreviousDateStringByMonth(String datetime, int months) {
        Calendar calendar = toCalendar(datetime);
        calendar.add(Calendar.MONTH, -months);

        return toDateString(calendar);
    }

    public static String toDateString(java.util.Date date, String spe) {
        if (date == null)
            return "";

        Calendar calendar = Calendar.getInstance();

        calendar.setTime(date);
        int month = calendar.get(Calendar.MONTH) + 1;
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        int year = calendar.get(Calendar.YEAR);

        String monthStr = "" + month;
        String dayStr = "" + day;
        String yearStr = "" + year;

        if (month < 10)
            monthStr = "0" + month;

        if (day < 10)
            dayStr = "0" + day;

        return yearStr + spe + monthStr + spe + dayStr;
    }

    public static String toDateString(java.util.Date date) {
        return toDateString(date, "-");
    }

    public static String toDateString(Calendar calendar) {
        return new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime());
    }

    public static Calendar toCalendar(String yearStr, String monthStr, String dayStr, String hourStr,
            String minuteStr, String secondStr) {
        int year, month, day, hour, minute, second;

        try {
            year = Integer.parseInt(yearStr);
            month = Integer.parseInt(monthStr);
            day = Integer.parseInt(dayStr);
            hour = Integer.parseInt(hourStr);
            minute = Integer.parseInt(minuteStr);
            second = Integer.parseInt(secondStr);
        } catch (Exception e) {
            return null;
        }

        return toCalendar(year, month, day, hour, minute, second);
    }

    public static Calendar toCalendar(String datetime) {
        int index = datetime.indexOf(" ");
        String date = datetime.substring(0, index);
        String time = datetime.substring(index + 1);

        int dateSlash1 = date.indexOf("-");
        int dateSlash2 = date.lastIndexOf("-");

        if (dateSlash1 <= 0 || dateSlash1 == dateSlash2)
            return null;

        int timeColon1 = time.indexOf(":");
        int timeColon2 = time.lastIndexOf(":");

        if (timeColon1 <= 0 || timeColon1 == timeColon2)
            return null;

        String yearStr = date.substring(0, dateSlash1);
        String monthStr = date.substring(dateSlash1 + 1, dateSlash2);
        String dayStr = date.substring(dateSlash2 + 1);

        String hourStr = time.substring(0, timeColon1);
        String minuteStr = time.substring(timeColon1 + 1, timeColon2);
        String secondStr = time.substring(timeColon2 + 1);
        ;

        int year, month, day, hour, minute, second;

        try {
            year = Integer.parseInt(yearStr);
            month = Integer.parseInt(monthStr);
            day = Integer.parseInt(dayStr);
            hour = Integer.parseInt(hourStr);
            minute = Integer.parseInt(minuteStr);
            second = Integer.parseInt(secondStr);
            Calendar calendar = Calendar.getInstance();

            calendar.set(year, month - 1, day, hour, minute, second);
            return calendar;
        } catch (Exception e) {
            return null;
        }
    }

    public static Calendar toCalendar(int[] dates) {
        if (dates == null || dates.length < 6)
            return null;

        return toCalendar(dates[0], dates[1], dates[2], dates[3], dates[4], dates[5]);
    }

    public static Calendar toCalendar(int year, int month, int day, int hour, int minute, int second) {
        Calendar c = Calendar.getInstance();
        c.set(Calendar.YEAR, year);
        c.set(Calendar.MONTH, month - 1);
        c.set(Calendar.DATE, day);
        c.set(Calendar.HOUR_OF_DAY, hour);
        c.set(Calendar.MINUTE, minute);
        c.set(Calendar.SECOND, second);

        return c;
    }
}

Related

  1. getNextMonthFistDay(String nowdate, String inFormat, String outFormat)
  2. getNextYearMonth(String yearMonth)
  3. getNrDaysOfMonth(int month, int year)
  4. getNumberOfDays(int year, int month)
  5. getOntologyURIBase(String defaultBase, boolean appendYear, boolean appendMonth, boolean appendDay)
  6. getPreviousDay(int year, int month, int day, int days)
  7. getPrevMonth(String divisionDate, int day, String outputFormat)
  8. getPreYearMonth()
  9. getSecondsOfMonth(int year, int month)