Java UTC Date getUTCCalendar()

Here you can find the source of getUTCCalendar()

Description

Retrieve an UTC Calendar.

License

Open Source License

Return

the uTC calendar

Declaration

public static Calendar getUTCCalendar() 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

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

public class Main {
    /** The Constant UTC_TIME_ZONE. */
    public final static TimeZone UTC_TIME_ZONE = TimeZone.getTimeZone("UTC");
    /** The Constant DATE_TIME_PATTERN. */
    public final static String DATE_TIME_PATTERN = "yyyy-MM-dd'T'HH:mm:ss'Z'";

    /**/*w  w w.jav a 2s.c  o m*/
     * Retrieve an UTC Calendar.
     *
     * @return the uTC calendar
     */
    public static Calendar getUTCCalendar() {
        return Calendar.getInstance(UTC_TIME_ZONE);
    }

    /**
     * Description goes here.
     *
     * @param date the date
     * @return the uTC calendar
     * @throws Exception the exception
     */
    public static Calendar getUTCCalendar(String date) throws Exception {
        Date parsedDate = parseDateTime(date);
        Calendar c = getUTCCalendar();
        c.setTime(parsedDate);
        return c;
    }

    /**
     * Description goes here.
     *
     * @param date the date
     * @return the date
     * @throws ParseException the parse exception
     */
    public static Date parseDateTime(String date) throws ParseException {
        if (date == null) {
            throw new NullPointerException("Provided date is null");
        }

        Date parsedDate = null;
        DateFormat dateFormat = new SimpleDateFormat(DATE_TIME_PATTERN, Locale.ENGLISH);
        dateFormat.setTimeZone(UTC_TIME_ZONE);

        try {
            parsedDate = dateFormat.parse(date);
            //         if (!sdf.getTimeZone().hasSameRules(DateUtils.UTC_TIME_ZONE)) {
            //            throw new Exception("The given date <" + date + "> is not in UTC time zone ");
            //         }
        } catch (ParseException e) {
            throw new ParseException("Impossible to parse the given string <" + date + "> into UTC date ",
                    e.getErrorOffset());
        }

        return parsedDate;
    }
}

Related

  1. formatUTC(Date time)
  2. formatUTC(Date time)
  3. getPlatypusConainerClass(Class aLayoutClass)
  4. getUTC()
  5. getUTC(String beforeFormart, String afterFormart, String dateStr)
  6. getUTCCalendar()
  7. getUTCCalendar(Calendar value)
  8. getUTCCalendarFromDialogString(String dateString)
  9. getUTCDate()