Java Date Now getCurrentDateTimeString()

Here you can find the source of getCurrentDateTimeString()

Description

get Current Date Time String

License

Apache License

Declaration

public static String getCurrentDateTimeString() 

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 SimpleDateFormat FORMAT_1 = new SimpleDateFormat(
            "MM-dd-yyyy hh:mm:ss a");

    public static String getCurrentDateTimeString() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.setTimeZone(TimeZone.getTimeZone("UTC"));

        return getDefaultFormatedDate(calendar.getTime().getTime());
    }/*ww w  .j a va  2s .  c o  m*/

    public static String getDefaultFormatedDate(Long ts) {
        if (ts == null)
            return "Null";

        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(ts);
        calendar.setTimeZone(TimeZone.getTimeZone("UTC"));

        Date date = calendar.getTime();

        return FORMAT_1.format(date);
    }

    public static long getTime(Date date, boolean isEnd) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        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. getCurrentDateTime(String dateFormat)
  2. getCurrentDateTime(String format)
  3. getCurrentDateTimeForShow()
  4. getCurrentDateTimeISO8601()
  5. getCurrentDateTimeObj(String dateTime, String format)
  6. getCurrentDateTimeString(String returnFormat)
  7. getCurrentDateTimeStringValue()
  8. getCurrentDateToString(String pattern)
  9. getCurrentDateWithDot()