Java Date Before getBeforeDate(String range)

Here you can find the source of getBeforeDate(String range)

Description

get Before Date

License

Open Source License

Declaration

public static Date getBeforeDate(String range) 

Method Source Code

//package com.java2s;

import java.util.Calendar;
import java.util.Date;

public class Main {
    public static Date getBeforeDate(String range) {
        Calendar today = Calendar.getInstance();
        if ("week".equalsIgnoreCase(range))
            today.add(4, -1);/*w w  w . ja v a  2  s  .c  om*/
        else if ("month".equalsIgnoreCase(range))
            today.add(2, -1);
        else
            today.clear();
        return today.getTime();
    }

    public static String getTime(int format) {
        StringBuffer cTime = new StringBuffer(10);
        Calendar time = Calendar.getInstance();
        int miltime = time.get(14);
        int second = time.get(13);
        int minute = time.get(12);
        int hour = time.get(11);
        int day = time.get(5);
        int month = time.get(2) + 1;
        int year = time.get(1);
        if (format != 14)
            if (year >= 2000)
                year -= 2000;
            else
                year -= 1900;

        if (format >= 2)
            if (format == 14)
                cTime.append(year);
            else
                cTime.append(getFormatTime(year, 2));

        if (format >= 4)
            cTime.append(getFormatTime(month, 2));
        if (format >= 6)
            cTime.append(getFormatTime(day, 2));
        if (format >= 8)
            cTime.append(getFormatTime(hour, 2));
        if (format >= 10)
            cTime.append(getFormatTime(minute, 2));
        if (format >= 12)
            cTime.append(getFormatTime(second, 2));
        if (format >= 15)
            cTime.append(getFormatTime(miltime, 3));
        return cTime.toString();
    }

    private static String getFormatTime(int time, int format) {
        StringBuffer numm = new StringBuffer();
        int length = String.valueOf(time).length();
        if (format < length)
            return null;
        for (int i = 0; i < format - length; ++i)
            numm.append("0");

        numm.append(time);
        return numm.toString().trim();
    }
}

Related

  1. beforeDays(Date date, Integer days)
  2. beforeTime(Date time1, Date time2)
  3. daysBefore(Date date1)
  4. getBefore(Date date)
  5. getBeforeDate(final int unit, final int period)
  6. getBeforeHourByCurrentTime(Date now, int mount)
  7. getBeforeOrAfterDateByDayNumber(int dayNumber)
  8. getDateBefore(Date d, int day)
  9. getDateBefore(Date d, int day)