Java String to Time getEndTime(String flag, int value)

Here you can find the source of getEndTime(String flag, int value)

Description

get End Time

License

Open Source License

Declaration

public static String getEndTime(String flag, int value) 

Method Source Code


//package com.java2s;

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

public class Main {

    public static String getEndTime(String flag, int value) {
        Calendar calendar = Calendar.getInstance();
        if (flag.equals("DAY")) {
            calendar.add(Calendar.DAY_OF_MONTH, -value);
        } else if (flag.equals("HOUR")) {
            calendar.add(Calendar.HOUR_OF_DAY, -value);
        } else if (flag.equals("MINUTE")) {
            calendar.add(Calendar.MINUTE, -value);
        }//from  w  ww.  j  a  v  a 2  s  . c  o m
        Date dateTime = calendar.getTime();
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(dateTime);
    }

    /**
     * Adds to a date returning a new object. The original date object is
     * unchanged.
     * 
     * @param date
     *            the date, not null
     * @param calendarField
     *            the calendar field to add to
     * @param amount
     *            the amount to add, may be negative
     * @return the new date object with the amount added
     * @throws IllegalArgumentException
     *             if the date is null
     */
    public static Date add(Date date, int calendarField, int amount) {
        if (date == null) {
            throw new IllegalArgumentException("The date must not be null");
        }
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(calendarField, amount);
        return c.getTime();
    }
}

Related

  1. getAsDateTime(String dateTime)
  2. getCalendarForSpecifiedDate(final String dateTime)
  3. getCalendarFromCPEGeneratorDateTime(String cpeDateTime)
  4. getCalendarFromDate(final String dateTime)
  5. getDlayDateTime(String playDate, String playTime)
  6. getExecutionDateTime(String input)
  7. getExpireTime(String timeStr)
  8. getFormerDate(String dateTime)
  9. getIdByTime(String name)