Example usage for java.util Calendar get

List of usage examples for java.util Calendar get

Introduction

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

Prototype

public int get(int field) 

Source Link

Document

Returns the value of the given calendar field.

Usage

From source file:Main.java

public static int GetYear(boolean lastTwoDigitOnly) {
    Calendar now = Calendar.getInstance();
    if (!lastTwoDigitOnly)
        return now.get(Calendar.YEAR);
    else/*  w ww  .  jav a2s .co  m*/
        return Integer.parseInt(String.valueOf(now.get(Calendar.YEAR)).substring(2, 4));
}

From source file:Main.java

public static double getJulDate() {
    Calendar cal = Calendar.getInstance();
    int year = cal.get(Calendar.YEAR);
    int month = cal.get(Calendar.MONTH) + 1;
    int day = cal.get(Calendar.DAY_OF_MONTH);
    int hour = cal.get(Calendar.HOUR_OF_DAY);
    int minute = cal.get(Calendar.MINUTE);
    int second = cal.get(Calendar.SECOND);
    double extra = (100.0 * year) + month - 190002.5;
    double julianDay = (367.0 * year) - (Math.floor(7.0 * (year + Math.floor((month + 9.0) / 12.0)) / 4.0))
            + Math.floor((275.0 * month) / 9.0) + day + ((hour + ((minute + (second / 60.0)) / 60.0)) / 24.0)
            + 1721013.5 - ((0.5 * extra) / Math.abs(extra)) + 0.5;
    DecimalFormat sixDigitFormat = new DecimalFormat("#.######");
    return Double.valueOf(sixDigitFormat.format(julianDay));
}

From source file:Main.java

public static String getGMTTimeString(long millisecond) {
    Calendar cal = Calendar.getInstance();
    int zoneOffsetTime = cal.get(Calendar.ZONE_OFFSET) + cal.get(Calendar.DST_OFFSET);
    long millisecondsUTC = millisecond - zoneOffsetTime;
    GMTTimeFormatter.setTimeZone(TimeZone.getDefault());
    String strGMTTime = GMTTimeFormatter.format(millisecondsUTC);
    return strGMTTime;
}

From source file:Main.java

public static boolean isDatesInSameWeek(long lhsDate, long rhsDate) {
    Calendar lhsCalendar = getCalendarWithTime(lhsDate);
    int lhsWeek = lhsCalendar.get(Calendar.WEEK_OF_YEAR);
    int lhsYear = lhsCalendar.get(Calendar.YEAR);

    Calendar rhsCalendar = getCalendarWithTime(rhsDate);
    int rhsWeek = rhsCalendar.get(Calendar.WEEK_OF_YEAR);
    int rhsYear = rhsCalendar.get(Calendar.YEAR);

    return (lhsWeek == rhsWeek) && (lhsYear == rhsYear);
}

From source file:Main.java

/** Convert a calendar date into a string as specified by http://tools.ietf.org/html/rfc822#section-5.1
 * @param date the calendar instance to convert to a string
 * @param locale the locale to use when outputting strings such as a day of the week, or month of the year.
 * @return a string in the format: "day_abbreviation, day_of_month month_abbreviation year hour:minute:second GMT"
 *///  w  ww  . j a v a 2 s.  c  o m
// package-private - unused
static final String convertDateToStringRfc822(Calendar date, Locale locale) {
    Date a = Date.from(Instant.now());
    a.toString();
    int day = date.get(Calendar.DAY_OF_MONTH);
    int hour = date.get(Calendar.HOUR_OF_DAY);
    int minute = date.get(Calendar.MINUTE);
    int second = date.get(Calendar.SECOND);
    String str = date.getDisplayName(Calendar.DAY_OF_WEEK, Calendar.SHORT, locale) + ", "
            + (day < 10 ? "0" + day : day) + " " + date.getDisplayName(Calendar.MONTH, Calendar.SHORT, locale)
            + " " + date.get(Calendar.YEAR) + " " + (hour < 10 ? "0" + hour : hour) + ":"
            + (minute < 10 ? "0" + minute : minute) + ":" + (second < 10 ? "0" + second : second) + " GMT";
    return str;
}

From source file:Main.java

public static long addUTCTime(long timeMillis) {

    Calendar cal = Calendar.getInstance(Locale.getDefault());
    int zoneOffset = cal.get(java.util.Calendar.ZONE_OFFSET);
    int dstOffset = cal.get(java.util.Calendar.DST_OFFSET);

    timeMillis = timeMillis + zoneOffset + dstOffset;//        if (start.after(end)) {

    return timeMillis;
}

From source file:Main.java

/**
 * Compares (only) the time of two Calendar objects
 * @param firstTime The first calendar object
 * @param secondTime The second calendar object
 * @return -1 if the time of the first calendar object is earlier than the second calendar object,
 *          1 if the time of the second calendar object is earlier than the first calendar object,
 *          0 if the times are equal//  www .j a va2  s .c o m
 */
public static int compareTime(Calendar firstTime, Calendar secondTime) {
    if (firstTime.get(Calendar.HOUR_OF_DAY) < secondTime.get(Calendar.HOUR_OF_DAY)) {
        return -1;
    } else if (firstTime.get(Calendar.HOUR_OF_DAY) > secondTime.get(Calendar.HOUR_OF_DAY)) {
        return 1;
    }
    if (firstTime.get(Calendar.MINUTE) < secondTime.get(Calendar.MINUTE)) {
        return -1;
    } else if (firstTime.get(Calendar.MINUTE) > secondTime.get(Calendar.MINUTE)) {
        return 1;
    }
    return 0;
}

From source file:Main.java

public static Calendar getRandomDate(int baseYear) {
    synchronized (rand) {
        Calendar today = Calendar.getInstance();
        int year = rand.nextInt(today.get(Calendar.YEAR) - baseYear + 1) + baseYear;
        int month = year == today.get(Calendar.YEAR) ? rand.nextInt(today.get(Calendar.MONTH) - 1) + 1
                : rand.nextInt(12) + 1;/* w  w  w .j  a  va  2 s  . c  om*/
        int day = year == today.get(Calendar.YEAR) ? rand.nextInt(today.get(Calendar.DAY_OF_WEEK)) + 1
                : rand.nextInt(29) + 1;

        return new GregorianCalendar(year, month, day);
    }
}

From source file:Main.java

public static String formatDate(Date date) {
    // TODO Auto-generated method stub
    Calendar calender = Calendar.getInstance();
    int thisyear = calender.get(Calendar.YEAR);
    calender.setTime(date);/*from   w w  w. j  a  va2  s.co m*/
    int dateyear = calender.get(Calendar.YEAR);
    if (thisyear == dateyear) {
        return DAY.format(date);
    }
    return DATE.format(date);
}

From source file:Main.java

public static int getMonthOfMonth(int y, int m) {
    Calendar cal = Calendar.getInstance();
    cal.set(y, m - 1, 1);/*  ww  w  .j  a  va 2s .c o m*/
    int dateOfMonth = cal.get(Calendar.MONTH);
    return dateOfMonth + 1;
}