Java Calendar Create getCalendar(String gdate)

Here you can find the source of getCalendar(String gdate)

Description

Get a Calendar for this gdate, or null in case of failure

License

Open Source License

Parameter

Parameter Description
gdate a parameter

Declaration

public static Calendar getCalendar(String gdate) 

Method Source Code

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

import java.util.Calendar;
import java.util.TimeZone;

public class Main {
    /**//from  w w  w .j a  v  a  2 s  . co  m
     * Get a Calendar for this gdate, or null in case of failure
     * @param gdate
     * @return
     */
    public static Calendar getCalendar(String gdate) {
        return getCalendar(gdate, null);
    }

    /**
     * Get a Calendar for this gdate, or ndef in case of failure
     * @param gdate
     * @param def
     * @return
     */
    public static Calendar getCalendar(String gdate, Calendar def) {
        if ((gdate != null) && (gdate.length() < 14))
            gdate = gdate + "00000000000000";
        if ((gdate == null) || (gdate.length() < 14)) {
            return def;
        }
        Calendar date = CalendargetInstance();
        try {
            date.set(Integer.parseInt(gdate.substring(0, 4)),
                    Integer.parseInt(gdate.substring(4, 6)) - 1,
                    Integer.parseInt(gdate.substring(6, 8)),
                    Integer.parseInt(gdate.substring(8, 10)),
                    Integer.parseInt(gdate.substring(10, 12)),
                    Integer.parseInt(gdate.substring(12, 14)));
            date.set(Calendar.MILLISECOND,
                    Integer.parseInt(gdate.substring(15, 18)));
        } catch (Exception e) {
            return def;
        }
        //System.out.println("getCalendar "+gdate+" into "+date);
        return date;
    }

    /**
     * Return now at UTC
     * @return
     */
    public static Calendar CalendargetInstance() {
        return Calendar.getInstance(TimeZone.getTimeZone("UTC"));
    }
}

Related

  1. getCalendar(long timeInMillis)
  2. getCalendar(long timeInMillis)
  3. getCalendar(String date)
  4. getCalendar(String dateStr, int inputYearType, int outputYearType)
  5. getCalendar(String dateString)
  6. getCalendar(String str)
  7. getCalendar(String time, char sep, boolean isStart)
  8. getCalendar(String yyyy, String mm, String dd)
  9. getCalendar(TimeZone timeZone, Locale locale)