List of usage examples for java.util GregorianCalendar BC
int BC
To view the source code for java.util GregorianCalendar BC.
Click Source Link
ERA
field indicating the period before the common era (before Christ), also known as BCE. From source file:Main.java
public static void main(String[] args) { GregorianCalendar cal = new GregorianCalendar(); cal.set(Calendar.ERA, GregorianCalendar.BC); System.out.println(cal.getTime()); }
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);//w ww .jav a 2 s .c o m String prefix = calendar.get(Calendar.ERA) == GregorianCalendar.BC ? "-" : "+"; SimpleDateFormat f = new SimpleDateFormat("yyyy"); gen.writeString(prefix + f.format(calendar.getTime())); }
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 ww w . j a v a 2s.co 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:org.apache.axis2.databinding.utils.ConverterUtil.java
/** * Converts a given string into a date. Code from Axis1 DateDeserializer. * * @param source/*from ww w.ja v a 2 s . co m*/ * @return Returns Date. */ public static Date convertToDate(String source) { // the lexical form of the date is '-'? yyyy '-' mm '-' dd zzzzzz? if ((source == null) || source.trim().equals("")) { return null; } source = source.trim(); boolean bc = false; if (source.startsWith("-")) { source = source.substring(1); bc = true; } int year = 0; int month = 0; int day = 0; int timeZoneOffSet = TimeZone.getDefault().getRawOffset(); if (source.length() >= 10) { //first 10 numbers must give the year if ((source.charAt(4) != '-') || (source.charAt(7) != '-')) { throw new RuntimeException("invalid date format (" + source + ") with out - s at correct place "); } year = Integer.parseInt(source.substring(0, 4)); month = Integer.parseInt(source.substring(5, 7)); day = Integer.parseInt(source.substring(8, 10)); if (source.length() > 10) { String restpart = source.substring(10); if (restpart.startsWith("Z")) { // this is a gmt time zone value timeZoneOffSet = 0; } else if (restpart.startsWith("+") || restpart.startsWith("-")) { // this is a specific time format string if (restpart.charAt(3) != ':') { throw new RuntimeException( "invalid time zone format (" + source + ") without : at correct place"); } int hours = Integer.parseInt(restpart.substring(1, 3)); int minits = Integer.parseInt(restpart.substring(4, 6)); timeZoneOffSet = ((hours * 60) + minits) * 60000; if (restpart.startsWith("-")) { timeZoneOffSet = timeZoneOffSet * -1; } } else { throw new RuntimeException("In valid string sufix"); } } } else { throw new RuntimeException("In valid string to parse"); } Calendar calendar = Calendar.getInstance(); calendar.clear(); calendar.setLenient(false); calendar.set(Calendar.YEAR, year); //xml month stars from the 1 and calendar month is starts with 0 calendar.set(Calendar.MONTH, month - 1); calendar.set(Calendar.DAY_OF_MONTH, day); calendar.set(Calendar.ZONE_OFFSET, timeZoneOffSet); // set the day light off set only if time zone if (source.length() >= 10) { calendar.set(Calendar.DST_OFFSET, 0); } calendar.getTimeInMillis(); if (bc) { calendar.set(Calendar.ERA, GregorianCalendar.BC); } return calendar.getTime(); }
From source file:org.apache.axis2.databinding.utils.ConverterUtil.java
/** * Code from Axis1 code base Note - We only follow the convention in the latest schema spec * * @param source/*from www. ja v a2 s.c om*/ * @return Returns Calendar. */ public static Calendar convertToDateTime(String source) { if ((source == null) || source.trim().equals("")) { return null; } source = source.trim(); // the lexical representation of the date time as follows // '-'? yyyy '-' mm '-' dd 'T' hh ':' mm ':' ss ('.' s+)? (zzzzzz)? Date date = null; Calendar calendar = Calendar.getInstance(); calendar.clear(); calendar.setLenient(false); if (source.startsWith("-")) { source = source.substring(1); calendar.set(Calendar.ERA, GregorianCalendar.BC); } int year = 0; int month = 0; int day = 0; int hour = 0; int minite = 0; int second = 0; long miliSecond = 0; int timeZoneOffSet = TimeZone.getDefault().getRawOffset(); if ((source != null) && (source.length() >= 19)) { if ((source.charAt(4) != '-') || (source.charAt(7) != '-') || (source.charAt(10) != 'T') || (source.charAt(13) != ':') || (source.charAt(16) != ':')) { throw new RuntimeException("invalid date format (" + source + ") with out - s at correct place "); } year = Integer.parseInt(source.substring(0, 4)); month = Integer.parseInt(source.substring(5, 7)); day = Integer.parseInt(source.substring(8, 10)); hour = Integer.parseInt(source.substring(11, 13)); minite = Integer.parseInt(source.substring(14, 16)); second = Integer.parseInt(source.substring(17, 19)); int milliSecondPartLength = 0; if (source.length() > 19) { String rest = source.substring(19); if (rest.startsWith(".")) { // i.e this have the ('.'s+) part if (rest.endsWith("Z")) { // this is in gmt time zone timeZoneOffSet = 0; calendar.setTimeZone(TimeZone.getTimeZone("GMT")); miliSecond = Integer.parseInt(rest.substring(1, rest.lastIndexOf("Z"))); milliSecondPartLength = rest.substring(1, rest.lastIndexOf("Z")).trim().length(); } else if ((rest.lastIndexOf("+") > 0) || (rest.lastIndexOf("-") > 0)) { // this is given in a general time zione String timeOffSet = null; if (rest.lastIndexOf("+") > 0) { timeOffSet = rest.substring(rest.lastIndexOf("+") + 1); miliSecond = Integer.parseInt(rest.substring(1, rest.lastIndexOf("+"))); milliSecondPartLength = rest.substring(1, rest.lastIndexOf("+")).trim().length(); // we keep +1 or -1 to finally calculate the value timeZoneOffSet = 1; } else if (rest.lastIndexOf("-") > 0) { timeOffSet = rest.substring(rest.lastIndexOf("-") + 1); miliSecond = Integer.parseInt(rest.substring(1, rest.lastIndexOf("-"))); milliSecondPartLength = rest.substring(1, rest.lastIndexOf("-")).trim().length(); // we keep +1 or -1 to finally calculate the value timeZoneOffSet = -1; } if (timeOffSet.charAt(2) != ':') { throw new RuntimeException( "invalid time zone format (" + source + ") without : at correct place"); } int hours = Integer.parseInt(timeOffSet.substring(0, 2)); int minits = Integer.parseInt(timeOffSet.substring(3, 5)); timeZoneOffSet = ((hours * 60) + minits) * 60000 * timeZoneOffSet; } else { // i.e it does not have time zone miliSecond = Integer.parseInt(rest.substring(1)); milliSecondPartLength = rest.substring(1).trim().length(); } } else { if (rest.startsWith("Z")) { calendar.setTimeZone(TimeZone.getTimeZone("GMT")); // this is in gmt time zone timeZoneOffSet = 0; } else if (rest.startsWith("+") || rest.startsWith("-")) { // this is given in a general time zione if (rest.charAt(3) != ':') { throw new RuntimeException( "invalid time zone format (" + source + ") without : at correct place"); } int hours = Integer.parseInt(rest.substring(1, 3)); int minits = Integer.parseInt(rest.substring(4, 6)); timeZoneOffSet = ((hours * 60) + minits) * 60000; if (rest.startsWith("-")) { timeZoneOffSet = timeZoneOffSet * -1; } } else { throw new NumberFormatException("in valid time zone attribute"); } } } calendar.set(Calendar.YEAR, year); // xml month is started from 1 and calendar month is started from 0 calendar.set(Calendar.MONTH, month - 1); calendar.set(Calendar.DAY_OF_MONTH, day); calendar.set(Calendar.HOUR_OF_DAY, hour); calendar.set(Calendar.MINUTE, minite); calendar.set(Calendar.SECOND, second); if (milliSecondPartLength != 3) { // milisecond part represenst the fraction of the second so we have to // find the fraction and multiply it by 1000. So if milisecond part // has three digits nothing required miliSecond = miliSecond * 1000; for (int i = 0; i < milliSecondPartLength; i++) { miliSecond = miliSecond / 10; } } calendar.set(Calendar.MILLISECOND, (int) miliSecond); calendar.set(Calendar.ZONE_OFFSET, timeZoneOffSet); // set the day light offset only if the time zone is present if (source.length() > 19) { calendar.set(Calendar.DST_OFFSET, 0); } } else { throw new NumberFormatException("date string can not be less than 19 characters"); } return calendar; }
From source file:org.apache.hadoop.hive.ql.udf.UDFADD_MONTHS.java
public Text evaluate(Text dateString1, IntWritable months) { if (dateString1 == null || months == null) { return null; }/* w ww .ja v a 2s . co m*/ try { Date inDate; boolean hasLine = false; for (int i = 0; i < dateString1.toString().length(); i++) { if (dateString1.toString().charAt(i) == '-') { hasLine = true; break; } } if (!hasLine) { if (dateString1.toString().trim().length() > 8) { return null; } inDate = formatterNoLine.parse(dateString1.toString().trim()); } else { inDate = formatter.parse(dateString1.toString()); } calendar.setTime(inDate); int fff = calendar.get(Calendar.ERA); if (fff == GregorianCalendar.BC) return null; int yyy = calendar.get(Calendar.YEAR); if ((yyy < 1900) || (yyy > 9999)) return null; int mmm = calendar.get(Calendar.MONTH); int rrr = yyy + (months.get() + mmm) / 12; if ((rrr < 1900) || (rrr > 9999)) return null; calendar.add(Calendar.DAY_OF_MONTH, 1); if (calendar.get(Calendar.DAY_OF_MONTH) == 1) { calendar.add(Calendar.MONTH, months.get()); calendar.add(Calendar.DAY_OF_MONTH, -1); } else { calendar.add(Calendar.DAY_OF_MONTH, -1); calendar.add(Calendar.MONTH, months.get()); } yyy = calendar.get(Calendar.YEAR); if ((yyy < 1900) || (yyy > 9999)) return null; Date newDate = calendar.getTime(); if (!hasLine) { result.set(formatterNoLine.format(newDate)); } else { result.set(formatter.format(newDate)); } return result; } catch (ParseException e) { return null; } }
From source file:org.apache.hadoop.hive.ql.udf.UDFLAST_DAY.java
public IntWritable evaluate(Text dateString) { if (dateString == null) { return null; }//w w w.j av a 2s . com 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.apache.hadoop.hive.ql.udf.UDFMONTHS_BETWEEN.java
public DoubleWritable evaluate(Text date1, Text date2) { if (date1 == null || date2 == null) { return null; }//from w w w . ja v a 2s . c o m try { Date date_1, date_2; boolean hasLine = false; for (int i = 0; i < date1.toString().length(); i++) { if (date1.toString().charAt(i) == '-') { hasLine = true; break; } } if (!hasLine) { if (date1.toString().trim().length() > 8) { return null; } date_1 = formatterNoLine.parse(date1.toString().trim()); } else { date_1 = formatter.parse(date1.toString()); } hasLine = false; for (int i = 0; i < date2.toString().length(); i++) { if (date2.toString().charAt(i) == '-') { hasLine = true; break; } } if (!hasLine) { if (date2.toString().trim().length() > 8) { return null; } date_2 = formatterNoLine.parse(date2.toString().trim()); } else { date_2 = formatter.parse(date2.toString()); } calendar.setTime(date_1); if (calendar.get(Calendar.ERA) == GregorianCalendar.BC) return null; int yyy = calendar.get(Calendar.YEAR); if ((yyy < 1900) || (yyy > 9999)) return null; int d_1 = calendar.get(Calendar.DAY_OF_MONTH); int m_1 = calendar.get(Calendar.MONTH); int y_1 = calendar.get(Calendar.YEAR); calendar.setTime(date_2); if (calendar.get(Calendar.ERA) == GregorianCalendar.BC) return null; yyy = calendar.get(Calendar.YEAR); if ((yyy < 1900) || (yyy > 9999)) return null; int d_2 = calendar.get(Calendar.DAY_OF_MONTH); int m_2 = calendar.get(Calendar.MONTH); int y_2 = calendar.get(Calendar.YEAR); double tmpR = (double) ((y_1 - y_2) * 12) + (double) (m_1 - m_2) + (double) (d_1 - d_2) / 31.0; result.set(tmpR); return result; } catch (ParseException e) { return null; } }
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 {//from w w w . ja va 2s . 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.apache.hadoop.hive.ql.udf.UDFTRUNC.java
public Text evaluate(Text date) { if (date == null) return null; try {/*from w w w.jav a2s . 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; 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; } catch (ParseException e) { return null; } }