Java TimeZone getDateAttributes(long millisec, TimeZone tz, boolean showNumber)

Here you can find the source of getDateAttributes(long millisec, TimeZone tz, boolean showNumber)

Description

get Date Attributes

License

Apache License

Declaration

public static Hashtable<String, String> getDateAttributes(long millisec, TimeZone tz, boolean showNumber) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.util.Calendar;
import java.util.Date;
import java.util.Hashtable;
import java.util.TimeZone;

public class Main {
    public static Hashtable<String, String> getDateAttributes(long millisec, TimeZone tz, boolean showNumber) {
        Calendar cal = null;/*  w ww . j a v  a 2 s.  co  m*/
        if (tz == null)
            cal = Calendar.getInstance();
        else
            cal = Calendar.getInstance(tz);
        cal.setLenient(true);
        cal.setTimeInMillis(millisec);
        java.util.Date d = cal.getTime();
        return getDateAttributes(d, tz, showNumber);
    }

    public static Hashtable<String, String> getDateAttributes(java.util.Date date, TimeZone tz,
            boolean showNumber) {
        Hashtable<String, String> datev = new Hashtable<String, String>();
        Calendar cal = null;
        if (date == null || (date instanceof Date) == false)
            return datev;
        if (tz == null)
            cal = Calendar.getInstance();
        else
            cal = Calendar.getInstance(tz);

        cal.setLenient(true);
        cal.setTime(date);

        try {
            int yr = cal.get(Calendar.YEAR);
            int month = cal.get(Calendar.MONTH);
            int day = cal.get(Calendar.DAY_OF_WEEK);
            int dayofmonth = cal.get(Calendar.DAY_OF_MONTH);
            int hour = cal.get(Calendar.HOUR_OF_DAY);
            int min = cal.get(Calendar.MINUTE);
            int sec = cal.get(Calendar.SECOND);
            int ampm = cal.get(Calendar.AM_PM);

            datev.put("year", new Integer(yr).toString());
            datev.put("month", getCanonicalMonth(month, showNumber));
            datev.put("day", getCanonicalDay(day, showNumber));
            datev.put("date", new Integer(dayofmonth).toString());
            datev.put("hour", new Integer(hour).toString());
            datev.put("minute", new Integer(min).toString());
            datev.put("second", new Integer(sec).toString());
            if (ampm == Calendar.AM)
                datev.put("ampm", "am");
            else
                datev.put("ampm", "pm");

        } catch (Exception e) {
            datev.put("exception", e.getLocalizedMessage());
            return datev;
        }

        return datev;
    }

    public static String getCanonicalMonth(int month, boolean isNumber) {
        switch (month) {
        case Calendar.JANUARY:
            return (isNumber == true) ? "1" : "January";
        case Calendar.FEBRUARY:
            return (isNumber == true) ? "2" : "February";
        case Calendar.MARCH:
            return (isNumber == true) ? "3" : "March";
        case Calendar.APRIL:
            return (isNumber == true) ? "4" : "April";
        case Calendar.MAY:
            return (isNumber == true) ? "5" : "May";
        case Calendar.JUNE:
            return (isNumber == true) ? "6" : "June";
        case Calendar.JULY:
            return (isNumber == true) ? "7" : "July";
        case Calendar.AUGUST:
            return (isNumber == true) ? "8" : "August";
        case Calendar.SEPTEMBER:
            return (isNumber == true) ? "9" : "September";
        case Calendar.OCTOBER:
            return (isNumber == true) ? "10" : "October";
        case Calendar.NOVEMBER:
            return (isNumber == true) ? "11" : "November";
        case Calendar.DECEMBER:
            return (isNumber == true) ? "12" : "December";
        case Calendar.UNDECIMBER:
            return (isNumber == true) ? "13" : "13Month";
        default:
            return (isNumber == true) ? "0" : "Undefined";
        }
    }

    public static String getCanonicalDay(int day, boolean isNumber) {
        switch (day) {
        case Calendar.SUNDAY:
            return (isNumber == true) ? "7" : "Sunday";
        case Calendar.MONDAY:
            return (isNumber == true) ? "1" : "Monday"; // Monday Starting day of week
        case Calendar.TUESDAY:
            return (isNumber == true) ? "2" : "Tuesday";
        case Calendar.WEDNESDAY:
            return (isNumber == true) ? "3" : "Wednesday";
        case Calendar.THURSDAY:
            return (isNumber == true) ? "4" : "Thursday";
        case Calendar.FRIDAY:
            return (isNumber == true) ? "5" : "Friday";
        case Calendar.SATURDAY:
            return (isNumber == true) ? "6" : "Saturday";
        default:
            return (isNumber == true) ? "0" : "Undefined";
        }
    }
}

Related

  1. extractTimeZone(String strdate, Date thedate)
  2. getDateAtTimeZone(Date when, TimeZone from, TimeZone to)
  3. getDateBoxValue(TimeZone zone, Date date)
  4. getDateValue(TimeZone zone, Long dateBoxValue, Long timeBoxValue)
  5. getDateWithoutTimeZone(Date date)
  6. getExcelSerialDayNumber(Date date, Locale locale, TimeZone timeZone)