com.coderoad.automation.common.util.DateUtil.java Source code

Java tutorial

Introduction

Here is the source code for com.coderoad.automation.common.util.DateUtil.java

Source

/*
 * DateUtil.java
 * Copyright (c) 2014, CODEROAD, All Rights Reserved.
 *
 * This software is the confidential and proprietary information of CODEROAD
 * ("Confidential Information"). You shall not disclose such Confidential Information and shall use
 * it only in accordance with the terms of the license agreement you entered into with
 * CODEROAD.
 */
package com.coderoad.automation.common.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.joda.time.LocalDateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

/**
 * The Class DateUtil.
 * 
 * @author $Author:$
 * @version $Rev:$ $Date:$
 */
public class DateUtil {

    /** The Constant DATE_ONLY_FMT. */
    public static final String DATE_ONLY_FMT = "MM/dd/yyyy";

    /** The Constant SMALL_FMT. */
    public static final String SMALL_FMT = "yyMMddHHmms";

    /** The Constant YEAR_MON_DATE_ONLY_FMT. */
    public static final String YEAR_MON_DATE_ONLY_FMT = "yyyy/MM/dd";

    /** The Constant DATE_FMT. */
    public static final String DATE_FMT = "yyyy-MM-dd HH:mm:ss";

    /** The Constant DATE_FMT_FULL. */
    public static final String DATE_FMT_FULL = "yyyy-MM-dd HH:mm:ss aa";

    /** The Constant DATE_FMT_FULL_TZ. */
    public static final String DATE_FMT_FULL_TZ = "yyyy-MM-dd HH:mm:ss aa Z";

    /** The Constant DATE_MONTH_YEAR_FMT. */
    public static final String DATE_MONTH_YEAR_FMT = "d MMMM yyyy";

    /** The Constant MM_DD_YY. */
    public static final String MM_DD_YY = "MM/dd/yy HH:mm aa";

    /** The Constant LARGE_FORMAT. */
    public static final String LARGE_FORMAT = "EEEE, MMMM dd, yyyy HH:mm:ss aa";

    /** The Constant HOUR_FORMAT. */
    public static final String HOUR_FORMAT = "HH:mm:ss aa";

    /** The Constant DATE_ONLY_FMT. */
    public static final String DATE_MONTH_ABBREVIATION = "dd MMM yyyy";

    public static final String TIME_FORMAT = "h:mm a";

    /**
     * Gets the current month.
     * 
     * @return the current month
     */
    public static String getCurrentMonth() {

        final String[] months = { "January", "February", "March", "April", "May", "June", "July", "August",
                "September", "October", "November", "December" };
        final Calendar cal = Calendar.getInstance();
        String month = "";
        month = months[cal.get(Calendar.MONTH)];
        return month;
    }

    /**
     * Gets the current date.
     * 
     * @return the current date
     */
    public static int getCurrentDate() {

        final Calendar now = Calendar.getInstance();
        int date = 0;
        date = now.get(Calendar.DATE);
        return date;
    }

    /**
     * Gets the current year.
     * 
     * @return the current year
     */
    public static int getCurrentYear() {

        final Calendar now = Calendar.getInstance();
        int year = 0;
        year = now.get(Calendar.YEAR);
        return year;
    }

    /**
     * Checks if is last date.
     * 
     * @return true, if is last date
     */
    public static boolean isLastDate() {

        DateTime startDate = new DateTime(DateTimeZone.UTC);
        int max = startDate.dayOfMonth().getMaximumValue();
        int current = startDate.getDayOfMonth();
        return current == max;
    }

    /**
     * Gets the last date of month.
     * 
     * @return the last date of month
     */
    public static Map<String, Integer> getLastDateOfMonth() {

        final HashMap<String, Integer> monthMap = new HashMap<String, Integer>();
        monthMap.put("January", 31);
        monthMap.put("February", 28);
        monthMap.put("March", 31);
        monthMap.put("April", 30);
        monthMap.put("May", 31);
        monthMap.put("June", 30);
        monthMap.put("July", 31);
        monthMap.put("August", 31);
        monthMap.put("September", 30);
        monthMap.put("October", 31);
        monthMap.put("November", 30);
        monthMap.put("December", 31);

        return monthMap;
    }

    /**
     * Difference in minutes.
     * 
     * @param date1 the date1
     * @param date2 the date2
     * @return the long
     */
    public static long differenceInMinutes(final Date date1, final Date date2) {

        final long diff = date2.getTime() - date1.getTime();
        return diff / (60 * 1000);
    }

    /**
     * Gets the calendar.
     * 
     * @param dateTime the date time
     * @param pattern the pattern
     * @return the calendar
     * @throws ParseException the parse exception
     */
    public static Calendar getCalendar(final String dateTime, String pattern) throws ParseException {

        DateTimeFormatter fmt = DateTimeFormat.forPattern(pattern);
        DateTime dt = fmt.parseDateTime(dateTime);
        return dt.toCalendar(Locale.ENGLISH);
    }

    /**
     * Gets the calendar.
     * 
     * @param dateTime the date time
     * @param pattern the pattern
     * @param timeZone the time zone
     * @return the calendar
     * @throws ParseException the parse exception
     */
    public static Calendar getCalendar(String dateTime, final String pattern, String timeZone)
            throws ParseException {

        DateTime dt = new DateTime(DateTimeZone.forID(timeZone));
        Calendar calendar = dt.toCalendar(Locale.ENGLISH);
        SimpleDateFormat sdf = new SimpleDateFormat(pattern, Locale.ENGLISH);
        Date date = sdf.parse(dateTime);
        calendar.setTime(date);
        return calendar;
    }

    /**
     * Gets the calendar.
     * 
     * @param startTime the start time
     * @param minutes the minutes
     * @param pattern the pattern
     * @return the calendar
     */
    public static Calendar getCalendar(Calendar startTime, int minutes, final String pattern) {

        Calendar endTime = null;
        DateTime start = new DateTime(startTime);
        DateTime end = start.plusMinutes(minutes);
        DateTimeFormatter fmt = DateTimeFormat.forPattern(pattern);
        fmt = fmt.withZoneUTC();
        endTime = parseToUTCCalendar(fmt.print(end), pattern);
        return endTime;
    }

    /**
     * Gets the calendar for time zone.
     * 
     * @param timeZone the time zone
     * @return the calendar for time zone
     * @throws ParseException the parse exception
     */
    public static Calendar getCalendarForTimeZone(final String timeZone) throws ParseException {

        DateTime dateTime = new DateTime(DateTimeZone.forID(timeZone));
        Calendar tzCalendar = dateTime.toCalendar(Locale.ENGLISH);
        return tzCalendar;
    }

    /**
     * Gets the calendar for date.
     * 
     * @param date the date
     * @return the calendar for date
     */
    public static Calendar getCalendarForDate(final Date date) {

        DateTime dt = new DateTime(date);
        Calendar calendar = dt.toCalendar(Locale.ENGLISH);
        calendar.setTime(date);
        return calendar;
    }

    /**
     * Copy to utc.
     * 
     * @param calendar the calendar
     * @param pattern the pattern
     * @return the calendar
     */
    public static Calendar copyToUTC(Calendar calendar, String pattern) {

        Calendar aCalendar = null;
        String date = format(calendar, pattern);
        aCalendar = parseToUTCCalendar(date, pattern);
        return aCalendar;
    }

    /**
     * Gets the UTC current time stamp.
     * 
     * @param pattern the pattern
     * @return the UTC current time stamp
     */
    public static String getUTCCurrentTimeStamp(String pattern) {

        DateTimeFormatter fmt = DateTimeFormat.forPattern(pattern);
        return fmt.print(new DateTime(DateTimeZone.UTC));
    }

    /**
     * Gets the current time stamp for zone.
     * 
     * @param timeZone the time zone
     * @param pattern the pattern
     * @return the current time stamp for zone
     */
    public static String getCurrentTimeStampForZone(String timeZone, String pattern) {

        DateTime startDate = new DateTime(DateTimeZone.forID(timeZone));
        DateTimeFormatter fmt = DateTimeFormat.forPattern(pattern);
        return fmt.print(startDate);
    }

    /**
     * Gets the UTC time from local.
     * 
     * @param localTimeInMillis the local time in millis
     * @param timeZone the time zone
     * @return the UTC time from local
     */
    public static long getUTCTimeFromLocal(long localTimeInMillis, String timeZone) {

        long utcTime = 0;
        DateTimeZone dateTimeZone = DateTimeZone.forID(timeZone);
        utcTime = dateTimeZone.convertLocalToUTC(localTimeInMillis, false);
        return utcTime;
    }

    /**
     * To date.
     * 
     * @param calendar the calendar
     * @return the date
     * @throws ParseException the parse exception
     */
    public static Date toDate(final Calendar calendar) throws ParseException {

        Date date = new Date();
        date = calendar.getTime();
        return date;
    }

    /**
     * To date with timezone.
     * 
     * @param dateTime the date time
     * @param pattern the pattern
     * @return the date
     * @throws ParseException the parse exception
     */
    public static Date toDateWithTimezone(String dateTime, String pattern) throws ParseException {

        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat(pattern, Locale.ENGLISH);
        date = sdf.parse(dateTime);
        return date;
    }

    /**
     * Format.
     * 
     * @param calendar the calendar
     * @param pattern the pattern
     * @return the string
     */
    public static String format(Calendar calendar, String pattern) {

        DateTimeFormatter fmt = DateTimeFormat.forPattern(pattern);
        return fmt.print(new DateTime(calendar));
    }

    /**
     * Format to utc.
     * 
     * @param calendar the calendar
     * @param pattern the pattern
     * @return the string
     */
    public static String formatToUTC(Calendar calendar, String pattern) {

        DateTimeFormatter fmt = DateTimeFormat.forPattern(pattern);
        fmt = fmt.withZoneUTC();
        String strDate = fmt.print(new DateTime(calendar));
        return strDate;
    }

    /**
     * Format to utc.
     * 
     * @param date the date
     * @param pattern the pattern
     * @return the string
     */
    public static String formatToUTC(Date date, String pattern) {

        DateTimeFormatter fmt = DateTimeFormat.forPattern(pattern);
        fmt = fmt.withZoneUTC();
        String strDate = fmt.print(new DateTime(date));
        return strDate;
    }

    /**
     * Format.
     * 
     * @param date the date
     * @param pattern the pattern
     * @return the string
     */
    public static String format(Date date, String pattern) {

        DateTimeFormatter fmt = DateTimeFormat.forPattern(pattern);
        String strDate = fmt.print(new DateTime(date));
        return strDate;
    }

    /**
     * Format to utc.
     * 
     * @param milliseconds the milliseconds
     * @param pattern the pattern
     * @return the string
     */
    public static String formatToUTC(Long milliseconds, String pattern) {

        DateTimeFormatter fmt = DateTimeFormat.forPattern(pattern);
        fmt = fmt.withZoneUTC();
        String strDate = fmt.print(new DateTime(milliseconds));
        return strDate;
    }

    /**
     * Format to zone.
     * 
     * @param milliseconds the milliseconds
     * @param zone the zone
     * @param pattern the pattern
     * @return the string
     */
    public static String formatToZone(Long milliseconds, DateTimeZone zone, String pattern) {

        DateTimeFormatter fmt = DateTimeFormat.forPattern(pattern);
        fmt = fmt.withZone(zone);
        String strDate = fmt.print(new DateTime(milliseconds));
        return strDate;
    }

    /**
     * Parses the to utc calendar.
     * 
     * @param timestamp the timestamp
     * @param pattern the pattern
     * @return the calendar
     */
    public static Calendar parseToUTCCalendar(String timestamp, String pattern) {

        DateTimeFormatter fmt = DateTimeFormat.forPattern(pattern);
        fmt = fmt.withZoneUTC();
        DateTime dt = fmt.parseDateTime(timestamp);
        dt = dt.toDateTime(DateTimeZone.UTC);
        return dt.toCalendar(Locale.ENGLISH);
    }

    /**
     * Parses the to utc date.
     * 
     * @param timestamp the timestamp
     * @param pattern the pattern
     * @return the date
     */
    public static Date parseToUTCDate(String timestamp, String pattern) {

        DateTimeFormatter fmt = DateTimeFormat.forPattern(pattern);
        fmt = fmt.withZoneUTC();
        Date date = fmt.parseDateTime(timestamp).toDate();
        return date;
    }

    /**
     * Parses the to utcms.
     * 
     * @param timestamp the timestamp
     * @param pattern the pattern
     * @return the long
     */
    public static Long parseToUTCMS(String timestamp, String pattern) {

        DateTimeFormatter fmt = DateTimeFormat.forPattern(pattern);
        fmt = fmt.withZoneUTC();
        Date date = fmt.parseDateTime(timestamp).toDate();
        return date.getTime();
    }

    /**
     * Convert timezone.
     * 
     * @param date the date
     * @param srcTimeZone the src time zone
     * @param destTimeZone the dest time zone
     * @return the date
     */
    public static Date convertTimezone(LocalDateTime date, String srcTimeZone, String destTimeZone) {

        DateTime srcDateTime = date.toDateTime(DateTimeZone.forID(srcTimeZone));
        DateTime dstDateTime = srcDateTime.withZone(DateTimeZone.forID(destTimeZone));
        return dstDateTime.toLocalDateTime().toDateTime().toDate();
    }

    /**
     * Gets the future date.
     * 
     * @return the future date
     */
    public static int getFutureDate() {

        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, 2);
        int date = calendar.get(Calendar.DATE);
        return date;
    }

    /**
     * Gets the future date from date.
     * 
     * @param selectedDate the selected date
     * @param daysCount the days count
     * @return the future date from date
     */
    public static int getFutureDateFromDate(int selectedDate, int daysCount) {

        Calendar calendar = Calendar.getInstance();
        if (selectedDate != 0) {
            calendar.set(Calendar.DATE, selectedDate);
        }
        calendar.add(Calendar.DATE, daysCount);
        int date = calendar.get(Calendar.DATE);
        return date;
    }

    /**
     * Gets the system time.
     * 
     * @return the system time
     */
    public static String getSystemTime(String pattern) {

        long secs = System.currentTimeMillis();
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        Date date = new Date(secs);
        return sdf.format(date);
    }
}