Java Month Get getTsOfMonth(Date date, boolean isEnd, boolean isPrevious)

Here you can find the source of getTsOfMonth(Date date, boolean isEnd, boolean isPrevious)

Description

get Ts Of Month

License

Apache License

Declaration

public static long getTsOfMonth(Date date, boolean isEnd,
            boolean isPrevious) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

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

public class Main {
    public static long getTsOfMonth(Date date, boolean isEnd,
            boolean isPrevious) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);/*from  w ww. ja v  a2s . co m*/
        calendar.setTimeZone(TimeZone.getTimeZone("UTC"));

        if (isEnd) {
            if (isPrevious) {
                calendar.add(Calendar.MONTH, -1);
            }
            calendar.set(Calendar.DATE,
                    calendar.getActualMaximum(Calendar.DATE));
            calendar.set(Calendar.HOUR_OF_DAY, 23);
            calendar.set(Calendar.MINUTE, 59);
            calendar.set(Calendar.SECOND, 59);
            calendar.set(Calendar.MILLISECOND, 999);
        } else {
            if (isPrevious) {
                calendar.add(Calendar.MONTH, -1);
            }
            calendar.set(Calendar.DATE, 1);
            calendar.set(Calendar.HOUR_OF_DAY, 0);
            calendar.set(Calendar.MINUTE, 0);
            calendar.set(Calendar.SECOND, 0);
            calendar.set(Calendar.MILLISECOND, 1);
        }
        return calendar.getTimeInMillis();
    }

    public static long getTimeInMillis(String dateString, String format) {
        long ts = 0L;

        try {
            SimpleDateFormat sdf = new SimpleDateFormat(format);

            Date date = sdf.parse(dateString);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.setTimeZone(TimeZone.getTimeZone("UTC"));
            calendar.set(Calendar.HOUR_OF_DAY, 0);
            calendar.set(Calendar.MINUTE, 0);
            calendar.set(Calendar.SECOND, 0);
            calendar.set(Calendar.MILLISECOND, 0);

            ts = calendar.getTimeInMillis();
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return ts;
    }
}

Related

  1. getThisMonth()
  2. getThisMonth()
  3. getThisMonthAndCycle()
  4. getThisMonthDate()
  5. getThisMonthLastDate()
  6. getWeekOfMonth(String datetime)
  7. getWorkMonthLastDay(Date date)
  8. getYesterdayOnLastMonth(String yesterday)
  9. getYestMonth()