Example usage for java.util GregorianCalendar AD

List of usage examples for java.util GregorianCalendar AD

Introduction

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

Prototype

int AD

To view the source code for java.util GregorianCalendar AD.

Click Source Link

Document

Value of the ERA field indicating the common era (Anno Domini), also known as CE.

Usage

From source file:Main.java

public static void main(String[] args) {

    GregorianCalendar cal = new GregorianCalendar();

    cal.set(Calendar.ERA, GregorianCalendar.AD);

    System.out.println(cal.getTime());

}

From source file:Main.java

public static void main(String args[]) {

    SimpleTimeZone stobj = new SimpleTimeZone(720, "US");

    // get offset
    int offset = stobj.getOffset(GregorianCalendar.AD, 2000, 10, 2, 4, 5000);

    // check offset value       
    System.out.println("Offset is : " + offset);
}

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 {//from w  w w.  j  a  v a2 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:edu.harvard.iq.dataverse.DatasetFieldValueValidator.java

private boolean isValidDate(String dateString, String pattern) {
    boolean valid = true;
    Date date;/*from  w w  w  .jav a 2 s  .c  o  m*/
    SimpleDateFormat sdf = new SimpleDateFormat(pattern);
    sdf.setLenient(false);
    try {
        dateString = dateString.trim();
        date = sdf.parse(dateString);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int year = calendar.get(Calendar.YEAR);
        int era = calendar.get(Calendar.ERA);
        if (era == GregorianCalendar.AD) {
            if (year > 9999) {
                valid = false;
            }
        }
    } catch (ParseException e) {
        valid = false;
    }
    if (dateString.length() > pattern.length()) {
        valid = false;
    }
    return valid;
}

From source file:TypeConversionHelper.java

/**
 * Converts a string in JDBC timestamp escape format to a Timestamp object.
 * To be precise, we prefer to find a JDBC escape type sequence in the format
 * "yyyy-mm-dd hh:mm:ss.fffffffff", but this does accept other separators
 * of fields, so as long as the numbers are in the order
 * year, month, day, hour, minute, second then we accept it.
 * @param s Timestamp string//from  w w  w  . j  a  va  2 s  . co m
 * @param cal The Calendar to use for conversion
 * @return Corresponding <tt>java.sql.Timestamp</tt> value.
 * @exception java.lang.IllegalArgumentException Thrown if the format of the
 * String is invalid
 */
public static Timestamp stringToTimestamp(String s, Calendar cal) {
    int[] numbers = convertStringToIntArray(s);
    if (numbers == null || numbers.length < 6) {
        throw new IllegalArgumentException("string to time stamp" + s);
    }

    int year = numbers[0];
    int month = numbers[1];
    int day = numbers[2];
    int hour = numbers[3];
    int minute = numbers[4];
    int second = numbers[5];
    int nanos = 0;
    if (numbers.length > 6) {
        nanos = numbers[6];
    }

    Calendar thecal = cal;
    if (cal == null) {
        thecal = new GregorianCalendar();
    }
    thecal.set(Calendar.ERA, GregorianCalendar.AD);
    thecal.set(Calendar.YEAR, year);
    thecal.set(Calendar.MONTH, month - 1);
    thecal.set(Calendar.DATE, day);
    thecal.set(Calendar.HOUR_OF_DAY, hour);
    thecal.set(Calendar.MINUTE, minute);
    thecal.set(Calendar.SECOND, second);
    Timestamp ts = new Timestamp(thecal.getTime().getTime());
    ts.setNanos(nanos);

    return ts;
}

From source file:org.apache.jackrabbit.core.persistence.util.BundleReader.java

/**
 * Deserializes a specially encoded date written using bundle
 * serialization version 3.//  w  ww . j  a va 2  s.  c  om
 *
 * @return deserialized date
 * @throws IOException if an I/O error occurs
 */
private Calendar readDate() throws IOException {
    long ts = readVarLong();

    TimeZone tz;
    if ((ts & 1) == 0) {
        tz = COMMON_TIMEZONES[0];
        ts >>= 1;
    } else if ((ts & 2) == 0) {
        tz = COMMON_TIMEZONES[((int) ts >> 2) & 0x1f]; // 5 bits;
        ts >>= 7;
    } else {
        int m = ((int) ts << 19) >> 21; // 11 bits, sign-extended
        int h = m / 60;
        String s;
        if (m < 0) {
            s = String.format("GMT-%02d:%02d", -h, h * 60 - m);
        } else {
            s = String.format("GMT+%02d:%02d", h, m - h * 60);
        }
        tz = TimeZone.getTimeZone(s);
        ts >>= 13;
    }

    int u = 0;
    int s = 0;
    int m = 0;
    int h = 0;
    int type = (int) ts & 3;
    ts >>= 2;
    switch (type) {
    case 3:
        u = (int) ts & 0x3fffffff; // 30 bits
        s = u / 1000;
        m = s / 60;
        h = m / 60;
        m -= h * 60;
        s -= (h * 60 + m) * 60;
        u -= ((h * 60 + m) * 60 + s) * 1000;
        ts >>= 30;
        break;
    case 2:
        m = (int) ts & 0x07ff; // 11 bits
        h = m / 60;
        m -= h * 60;
        ts >>= 11;
        break;
    case 1:
        h = (int) ts & 0x1f; // 5 bits
        ts >>= 5;
        break;
    }

    int d = (int) ts & 0x01ff; // 9 bits;
    ts >>= 9;
    int y = (int) (ts + 2010);

    Calendar value = Calendar.getInstance(tz);
    if (y <= 0) {
        value.set(Calendar.YEAR, 1 - y);
        value.set(Calendar.ERA, GregorianCalendar.BC);
    } else {
        value.set(Calendar.YEAR, y);
        value.set(Calendar.ERA, GregorianCalendar.AD);
    }
    value.set(Calendar.DAY_OF_YEAR, d);
    value.set(Calendar.HOUR_OF_DAY, h);
    value.set(Calendar.MINUTE, m);
    value.set(Calendar.SECOND, s);
    value.set(Calendar.MILLISECOND, u);

    return value;
}

From source file:org.apache.logging.log4j.core.util.datetime.FastDateParserTest.java

@Test
public void testParseLongShort() throws ParseException {
    final Calendar cal = Calendar.getInstance(NEW_YORK, Locale.US);
    cal.clear();/* ww  w. j av  a2 s .  com*/
    cal.set(2003, Calendar.FEBRUARY, 10, 15, 33, 20);
    cal.set(Calendar.MILLISECOND, 989);
    cal.setTimeZone(NEW_YORK);

    DateParser fdf = getInstance("yyyy GGGG MMMM dddd aaaa EEEE HHHH mmmm ssss SSSS ZZZZ", NEW_YORK, Locale.US);

    assertEquals(cal.getTime(), fdf.parse("2003 AD February 0010 PM Monday 0015 0033 0020 0989 GMT-05:00"));
    cal.set(Calendar.ERA, GregorianCalendar.BC);

    final Date parse = fdf.parse("2003 BC February 0010 PM Saturday 0015 0033 0020 0989 GMT-05:00");
    assertEquals(cal.getTime(), parse);

    fdf = getInstance("y G M d a E H m s S Z", NEW_YORK, Locale.US);
    assertEquals(cal.getTime(), fdf.parse("03 BC 2 10 PM Sat 15 33 20 989 -0500"));

    cal.set(Calendar.ERA, GregorianCalendar.AD);
    assertEquals(cal.getTime(), fdf.parse("03 AD 2 10 PM Saturday 15 33 20 989 -0500"));
}