Java Date to Time getTime(Date date, boolean isEnd)

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

Description

get Time

License

Apache License

Declaration

public static long getTime(Date date, boolean isEnd) 

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 getTime(Date date, boolean isEnd) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);/*w ww  .j a  v a  2  s  .  c o  m*/
        calendar.setTimeZone(TimeZone.getTimeZone("UTC"));

        if (isEnd) {
            calendar.set(Calendar.HOUR_OF_DAY, 23);
            calendar.set(Calendar.MINUTE, 59);
            calendar.set(Calendar.SECOND, 59);
            calendar.set(Calendar.MILLISECOND, 999);
        } else {
            calendar.set(Calendar.HOUR_OF_DAY, 0);
            calendar.set(Calendar.MINUTE, 0);
            calendar.set(Calendar.SECOND, 0);
            calendar.set(Calendar.MILLISECOND, 0);
        }
        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. getTime(Date date)
  2. getTime(Date date)
  3. getTime(Date date)
  4. getTime(Date date)
  5. getTime(Date date)
  6. getTime(Date date, String pattern, Locale locale)
  7. getTime(Date dateParam)
  8. getTime(Date executionTime)
  9. getTime(Date timestamp)