Example usage for java.util GregorianCalendar get

List of usage examples for java.util GregorianCalendar get

Introduction

In this page you can find the example usage for java.util GregorianCalendar get.

Prototype

public int get(int field) 

Source Link

Document

Returns the value of the given calendar field.

Usage

From source file:Main.java

protected static String toString(GregorianCalendar g1) {
    StringBuilder buffer = new StringBuilder();

    buffer.append(//from w w w .  ja v a  2 s  .c  om
            g1.get(Calendar.YEAR) + "/" + (g1.get(Calendar.MONTH) + 1) + "/" + g1.get(Calendar.DAY_OF_MONTH));
    buffer.append("  ");
    buffer.append(g1.get(Calendar.HOUR_OF_DAY) + ":" + g1.get(Calendar.MINUTE) + ":" + g1.get(Calendar.SECOND)
            + "." + g1.get(Calendar.MILLISECOND));

    return buffer.toString();
}

From source file:Main.java

/**
 * Returns the year (index 0), month (index 1) and day (index 2) of the given timestamp.
 * @param timestamp the timestamp//from w w  w .j  a  v  a  2 s.com
 * @return the year (index 0), month (index 1) and day (index 2) of the given timestamp
 */
public static int[] getDayMonthYear(long timestamp) {
    GregorianCalendar gregorianCalendar = new GregorianCalendar();
    gregorianCalendar.setTimeInMillis(timestamp);

    int day = gregorianCalendar.get(Calendar.DAY_OF_MONTH);
    int month = gregorianCalendar.get(Calendar.MONTH) + 1;
    int year = gregorianCalendar.get(Calendar.YEAR);

    return new int[] { year, month, day };
}

From source file:DateUtils.java

/**
 * A method to get the current date as an array of components
 * 0 = year, 1 = month, 2 = day/*from  www .j  av a 2s. c om*/
 *
 * @return an array of strings representing the current date
 */
public static String[] getCurrentDateAsArray() {

    GregorianCalendar calendar = new GregorianCalendar();
    String[] fields = new String[3];

    fields[0] = Integer.toString(calendar.get(Calendar.YEAR));
    fields[1] = String.format("%02d", calendar.get(Calendar.MONTH) + 1);
    fields[2] = String.format("%02d", calendar.get(Calendar.DAY_OF_MONTH));

    return fields;
}

From source file:TimeUtil.java

public static String monthStringFormat(long msecs) {
    GregorianCalendar cal = new GregorianCalendar();

    cal.setTime(new Date(msecs));

    return monthStringFormat(cal.get(Calendar.MONTH));
}

From source file:TimeUtil.java

public static String dayStringFormat(long msecs) {
    GregorianCalendar cal = new GregorianCalendar();

    cal.setTime(new Date(msecs));

    int dow = cal.get(Calendar.DAY_OF_WEEK);

    switch (dow) {
    case Calendar.MONDAY:
        return "Monday";
    case Calendar.TUESDAY:
        return "Tuesday";
    case Calendar.WEDNESDAY:
        return "Wednesday";
    case Calendar.THURSDAY:
        return "Thursday";
    case Calendar.FRIDAY:
        return "Friday";
    case Calendar.SATURDAY:
        return "Saturday";
    case Calendar.SUNDAY:
        return "Sunday";
    }/*from w  w  w  .  j  ava 2 s .  co m*/

    return "Unknown";
}

From source file:Main.java

/**
 * This method converts from GMT to local timezone
 * //from  w w w.  j ava  2  s . c o m
 * @param date
 *       date in GMT timezone
 * @return the date in local timezone
 */
public static Date offsetDateFromGMT(final Date date) {
    // Create a calendar - it will default to the current OS timezone.
    final GregorianCalendar gc = new GregorianCalendar();

    // Calculate the total offset from GMT
    final int totalOffset = gc.get(Calendar.ZONE_OFFSET) + gc.get(Calendar.DST_OFFSET);

    // Calculate the time in GMT
    final long localTime = date.getTime() + totalOffset;

    // Create a date using the calculated GMT time
    final Date localDate = new Date(localTime);

    return localDate;
}

From source file:Main.java

public static long convertTime(long time) {
    GregorianCalendar t1 = new GregorianCalendar(TimeZone.getTimeZone("America/Los_Angeles"));
    t1.setTimeInMillis(time);//  www .  ja v a  2s  .c om
    GregorianCalendar t2 = new GregorianCalendar();
    t2.set(t1.get(GregorianCalendar.YEAR), t1.get(GregorianCalendar.MONTH),
            t1.get(GregorianCalendar.DAY_OF_MONTH), t1.get(GregorianCalendar.HOUR_OF_DAY),
            t1.get(GregorianCalendar.MINUTE), t1.get(GregorianCalendar.SECOND));
    return t2.getTimeInMillis();
}

From source file:Main.java

public static XMLGregorianCalendar asXMLGregorianCalendar(java.util.Date date) {
    if (date == null) {
        return null;
    } else {//from w w  w .j  a v a 2  s  . c  o  m
        GregorianCalendar calendar = new GregorianCalendar();
        calendar.setTime(date);

        XMLGregorianCalendar xmlGregorianCalendar = df.newXMLGregorianCalendar();
        xmlGregorianCalendar.setDay(calendar.get(Calendar.DAY_OF_MONTH));
        xmlGregorianCalendar.setMonth(calendar.get(Calendar.MONTH));
        xmlGregorianCalendar.setYear(calendar.get(Calendar.YEAR));

        return xmlGregorianCalendar;
    }
}

From source file:TimeUtil.java

/**
 * convert time in milliseconds into a display string of the form [h]h:mm
 * [am|pm] (traditional) or hh:mm (24 hour format) if using traditional
 * format, the leading 'h' & 'm' will be padded with a space to ensure
 * constant length if less than 10 24 hour format
 * /*from w ww .  j a  v a 2  s .co m*/
 * @param msecs
 *            a millisecond time
 * @return TimeString the formatted time string
 */
public static String stringFormat(long msecs) {
    GregorianCalendar cal = new GregorianCalendar();
    StringBuffer sBuf = new StringBuffer(8);

    cal.setTime(new Date(msecs));

    int hour = cal.get(Calendar.HOUR);

    if (hour == 0)
        hour = 12;

    if (hour < 10)
        sBuf.append(" ");

    sBuf.append(Integer.toString(hour));
    sBuf.append(":");

    int minute = cal.get(Calendar.MINUTE);

    if (minute < 10)
        sBuf.append("0");

    sBuf.append(Integer.toString(minute));
    sBuf.append(" ");
    sBuf.append(cal.get(Calendar.AM_PM) == Calendar.AM ? "AM" : "PM");

    return (sBuf.toString());
}

From source file:Main.java

public static String weekDay(String year, String month, String day) {
    String strWeekday = "";
    try {/*w  ww .  j  a  v a 2s  .c  om*/
        GregorianCalendar cal = new GregorianCalendar();
        cal.setLenient(false);
        cal.clear();
        cal.set(Integer.parseInt(year), Integer.parseInt(month) - 1, Integer.parseInt(day));
        strWeekday = String.valueOf(cal.get(Calendar.DAY_OF_WEEK) - 1);
    } catch (IllegalArgumentException e) {
        strWeekday = "";
    }
    return strWeekday;
}