Java Date to Month getMonth(long date, int increment)

Here you can find the source of getMonth(long date, int increment)

Description

get Month

License

Open Source License

Declaration

private static long getMonth(long date, int increment) 

Method Source Code


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

public class Main {
    private static Calendar CALENDAR = Calendar.getInstance();

    private static long getMonth(long date, int increment) {
        long result;
        Calendar calendar = CALENDAR;
        synchronized (calendar) {
            calendar.setTimeInMillis(date);
            if (increment == -1) {
                calendar.set(Calendar.DAY_OF_MONTH, 1);
                result = startOfDayInMillis(calendar.getTimeInMillis());
            } else {
                calendar.add(Calendar.MONTH, 1);
                calendar.set(Calendar.DAY_OF_MONTH, 1);
                calendar.set(Calendar.HOUR_OF_DAY, 0);
                calendar.set(Calendar.MILLISECOND, 0);
                calendar.set(Calendar.SECOND, 0);
                calendar.set(Calendar.MINUTE, 0);
                calendar.add(Calendar.MILLISECOND, -1);
                result = calendar.getTimeInMillis();
            }/*from  ww w .  j  a  v  a 2 s  .co m*/
        }
        return result;
    }

    /**
    * Returns day in millis with the hours, milliseconds, seconds and minutes
    * set to 0.
    *
    * @param date long used in calculating start of day
    * @return Start of <code>date</code>
    */
    public static long startOfDayInMillis(long date) {
        Calendar calendar = CALENDAR;
        synchronized (calendar) {
            calendar.setTimeInMillis(date);
            calendar.set(Calendar.HOUR_OF_DAY, 0);
            calendar.set(Calendar.MILLISECOND, 0);
            calendar.set(Calendar.SECOND, 0);
            calendar.set(Calendar.MINUTE, 0);
            return calendar.getTimeInMillis();
        }
    }
}

Related

  1. getMonth(Date... objects)
  2. getMonth(java.util.Date date)
  3. getMonth(java.util.Date date)
  4. getMonth(java.util.Date date)
  5. getMonth(java.util.Date today)
  6. getMonth(String strDate)
  7. getMonthBeginDate()
  8. getMonthBetween(Date data1, Date data2)
  9. getMonthByOffset(Date date, int offset)