Example usage for java.util Calendar ERA

List of usage examples for java.util Calendar ERA

Introduction

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

Prototype

int ERA

To view the source code for java.util Calendar ERA.

Click Source Link

Document

Field number for get and set indicating the era, e.g., AD or BC in the Julian calendar.

Usage

From source file:Main.java

static boolean isSameDay(Calendar cal1, Calendar cal2) {
    if (cal1 == null || cal2 == null) {
        return false;
    }//from   w w  w  .  j  ava 2s  . co  m

    return (cal1.get(Calendar.ERA) == cal2.get(Calendar.ERA)
            && cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR)
            && cal1.get(Calendar.DAY_OF_YEAR) == cal2.get(Calendar.DAY_OF_YEAR));
}

From source file:DateUtils.java

/**
 * <p>Checks if two calendars represent the same day ignoring time.</p>
 * @param cal1  the first calendar, not altered, not null
 * @param cal2  the second calendar, not altered, not null
 * @return true if they represent the same day
 * @throws IllegalArgumentException if either calendar is <code>null</code>
 *//*ww w  .  j a v  a 2  s.co  m*/
public static boolean isSameDay(Calendar cal1, Calendar cal2) {
    if (cal1 == null || cal2 == null) {
        throw new IllegalArgumentException("The dates must not be null");
    }
    return (cal1.get(Calendar.ERA) == cal2.get(Calendar.ERA)
            && cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR)
            && cal1.get(Calendar.DAY_OF_YEAR) == cal2.get(Calendar.DAY_OF_YEAR));
}

From source file:CalendarDemo.java

public void format() {

    // Tell the calendar what date/time to format
    calendar.setTime(timeNow);/*from ww w.  j  a  va  2 s.c o  m*/

    // print out most of the known fields
    System.out.println("ERA: " + calendar.get(Calendar.ERA));
    System.out.println("YEAR: " + calendar.get(Calendar.YEAR));
    System.out.println("MONTH: " + calendar.get(Calendar.MONTH));
    System.out.println("WEEK_OF_YEAR: " + calendar.get(Calendar.WEEK_OF_YEAR));
    System.out.println("WEEK_OF_MONTH: " + calendar.get(Calendar.WEEK_OF_MONTH));
    System.out.println("DATE: " + calendar.get(Calendar.DATE));
    System.out.println("DAY_OF_MONTH: " + calendar.get(Calendar.DAY_OF_MONTH));
    System.out.println("DAY_OF_YEAR: " + calendar.get(Calendar.DAY_OF_YEAR));
    System.out.println("DAY_OF_WEEK: " + calendar.get(Calendar.DAY_OF_WEEK));
    System.out.println("DAY_OF_WEEK_IN_MONTH: " + calendar.get(Calendar.DAY_OF_WEEK_IN_MONTH));
    System.out.println("AM_PM: " + calendar.get(Calendar.AM_PM));
    System.out.println("HOUR: " + calendar.get(Calendar.HOUR));
    System.out.println("HOUR_OF_DAY: " + calendar.get(Calendar.HOUR_OF_DAY));
    System.out.println("MINUTE: " + calendar.get(Calendar.MINUTE));
    System.out.println("SECOND: " + calendar.get(Calendar.SECOND));
    System.out.println("MILLISECOND: " + calendar.get(Calendar.MILLISECOND));
    System.out.println("ZONE_OFFSET: " + (calendar.get(Calendar.ZONE_OFFSET) / (60 * 60 * 1000)));
    System.out.println("DST_OFFSET: " + (calendar.get(Calendar.DST_OFFSET) / (60 * 60 * 1000)));
}

From source file:org.comicwiki.serializers.YearSerializer.java

@Override
public void serialize(Date value, JsonGenerator gen, SerializerProvider provider) throws IOException {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(value);//from   w  w  w  . j  a  v  a  2s  . c om

    String prefix = calendar.get(Calendar.ERA) == GregorianCalendar.BC ? "-" : "+";
    SimpleDateFormat f = new SimpleDateFormat("yyyy");
    gen.writeString(prefix + f.format(calendar.getTime()));
}

From source file:org.kalypso.commons.time.PeriodUtils.java

public static Period getPeriod(final int calendarField, final int amount) {
    switch (calendarField) {
    case Calendar.YEAR:
        return Period.years(amount);

    case Calendar.MONTH:
        return Period.months(amount);

    case Calendar.WEEK_OF_YEAR:
    case Calendar.WEEK_OF_MONTH:
        return Period.weeks(amount);

    case Calendar.DAY_OF_MONTH:
    case Calendar.DAY_OF_YEAR:
    case Calendar.DAY_OF_WEEK:
    case Calendar.DAY_OF_WEEK_IN_MONTH:
        return Period.days(amount);

    case Calendar.HOUR:
    case Calendar.HOUR_OF_DAY:
        return Period.hours(amount);

    case Calendar.MINUTE:
        return Period.minutes(amount);

    case Calendar.SECOND:
        return Period.seconds(amount);

    case Calendar.MILLISECOND:
        return Period.millis(amount);

    case Calendar.AM_PM:
    case Calendar.ERA:
    default:/* ww w .j a  v  a  2  s.  c  o m*/
        throw new UnsupportedOperationException();
    }
}

From source file:org.apache.hadoop.hive.ql.udf.UDFLAST_DAY.java

public IntWritable evaluate(Text dateString) {

    if (dateString == null) {
        return null;
    }/*from   w  ww  .  j av a2  s.c  o m*/

    try {
        Date date = formatter.parse(dateString.toString());
        calendar.setTime(date);

        if (calendar.get(Calendar.ERA) == GregorianCalendar.BC)
            return null;
        int xxh = calendar.get(Calendar.YEAR);
        if ((xxh < 1900) || (xxh > 9999))
            return null;

        int maxDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
        result.set(maxDay);
        return result;
    } catch (ParseException e) {
        return null;
    }
}

From source file:org.comicwiki.serializers.YearDeserializer.java

@Override
public Date deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
    ObjectMapper mapper = (ObjectMapper) p.getCodec();
    JsonNode valueNode = mapper.readTree(p);
    String dateText = valueNode.asText();
    try {/*w  w w  . ja v a 2  s.  c o  m*/
        Date time = new SimpleDateFormat("yyyy").parse(dateText);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(time);
        calendar.set(Calendar.ERA, (dateText.charAt(0) == '+') ? GregorianCalendar.AD : GregorianCalendar.BC);
        return calendar.getTime();
    } catch (ParseException e) {
        throw new IOException(e);
    }
}

From source file:Main.java

/**
 * <p>Checks if two calendar objects represent the same local time.</p>
 *
 * <p>This method compares the values of the fields of the two objects.
 * In addition, both calendars must be the same of the same type.</p>
 * //w  w w.  j av a 2  s  . co  m
 * @param cal1  the first calendar, not altered, not null
 * @param cal2  the second calendar, not altered, not null
 * @return true if they represent the same millisecond instant
 * @throws IllegalArgumentException if either date is <code>null</code>
 * @since 2.1
 */
public static boolean isSameLocalTime(Calendar cal1, Calendar cal2) {
    if (cal1 == null || cal2 == null) {
        throw new IllegalArgumentException("The date must not be null");
    }
    return (cal1.get(Calendar.MILLISECOND) == cal2.get(Calendar.MILLISECOND)
            && cal1.get(Calendar.SECOND) == cal2.get(Calendar.SECOND)
            && cal1.get(Calendar.MINUTE) == cal2.get(Calendar.MINUTE)
            && cal1.get(Calendar.HOUR) == cal2.get(Calendar.HOUR)
            && cal1.get(Calendar.DAY_OF_YEAR) == cal2.get(Calendar.DAY_OF_YEAR)
            && cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR)
            && cal1.get(Calendar.ERA) == cal2.get(Calendar.ERA) && cal1.getClass() == cal2.getClass());
}

From source file:org.apache.hadoop.hive.ql.udf.UDFTRUNC.java

public Text evaluate(Text date, Text format) {
    if (date == null || format == null)
        return null;
    try {//w w  w  .jav a  2 s . c o  m
        Date theDate = formatter.parse(date.toString());
        calendar.setTime(theDate);

        if (calendar.get(Calendar.ERA) == GregorianCalendar.BC)
            return null;
        int xxh = calendar.get(Calendar.YEAR);
        if ((xxh < 1900) || (xxh > 9999))
            return null;

        String fff = format.toString();
        if (fff.equals("YEAR") || fff.equals("YYYY") || fff.equals("YYY") || fff.equals("YY") || fff.equals("Y")
                || fff.equals("SYEAR") || fff.equals("SYYYY")) {
            calendar.set(calendar.get(Calendar.YEAR), 0, 1, 0, 0, 0);
            result.set(formatter.format(calendar.getTime()));
            return result;
        } else if (fff.equals("MONTH") || fff.equals("MON") || fff.equals("MM") || fff.equals("RM")) {
            calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), 1, 0, 0, 0);
            result.set(formatter.format(calendar.getTime()));
            return result;
        } else if (fff.equals("WW")) {
            calendar.set(Calendar.HOUR, 0);
            calendar.set(Calendar.MINUTE, 0);
            calendar.set(Calendar.SECOND, 0);
            if (calendar.get(Calendar.DAY_OF_YEAR) % 7 == 0)
                calendar.set(Calendar.DAY_OF_YEAR, ((calendar.get(Calendar.DAY_OF_YEAR) / 7 - 1) * 7 + 1));
            else
                calendar.set(Calendar.DAY_OF_YEAR, ((calendar.get(Calendar.DAY_OF_YEAR) / 7) * 7 + 1));
            result.set(formatter.format(calendar.getTime()));
            return result;
        } else if (fff.equals("W")) {
            calendar.set(Calendar.HOUR, 0);
            calendar.set(Calendar.MINUTE, 0);
            calendar.set(Calendar.SECOND, 0);
            if (calendar.get(Calendar.DAY_OF_MONTH) % 7 == 0)
                calendar.set(Calendar.DAY_OF_MONTH, ((calendar.get(Calendar.DAY_OF_MONTH) / 7 - 1) * 7 + 1));
            else
                calendar.set(Calendar.DAY_OF_MONTH, ((calendar.get(Calendar.DAY_OF_MONTH) / 7) * 7 + 1));
            result.set(formatter.format(calendar.getTime()));
            return result;
        } else if (fff.equals("DAY") || fff.equals("D") || fff.equals("DY")) {
            calendar.set(Calendar.DAY_OF_MONTH,
                    calendar.get(Calendar.DAY_OF_MONTH) + 1 - calendar.get(Calendar.DAY_OF_WEEK));
            calendar.set(Calendar.HOUR, 0);
            calendar.set(Calendar.MINUTE, 0);
            calendar.set(Calendar.SECOND, 0);
            result.set(formatter.format(calendar.getTime()));
            return result;
        } else if (fff.equals("DDD") || fff.equals("DD") || fff.equals("J")) {
            calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),
                    calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
            result.set(formatter.format(calendar.getTime()));
            return result;
        } else if (fff.equals("HH") || fff.equals("HH12") || fff.equals("HH24")) {
            calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),
                    calendar.get(Calendar.DAY_OF_MONTH), calendar.get(Calendar.HOUR_OF_DAY), 0, 0);
            result.set(formatter.format(calendar.getTime()));
            return result;
        } else if (fff.equals("MI")) {
            calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),
                    calendar.get(Calendar.DAY_OF_MONTH), calendar.get(Calendar.HOUR_OF_DAY),
                    calendar.get(Calendar.MINUTE), 0);
            result.set(formatter.format(calendar.getTime()));
            return result;
        } else {
            return null;
        }
    } catch (ParseException e) {
        return null;
    }
}

From source file:org.mule.el.datetime.AbstractInstant.java

@Override
public Instant changeTimeZone(String newTimezone) {
    // Workaround for issues with java.util.Calendar.  Ensure Calendar is in right state before updating timeZone 
    calendar.get(Calendar.ERA);
    calendar.setTimeZone(TimeZone.getTimeZone(newTimezone));
    return this;
}