List of usage examples for java.util GregorianCalendar GregorianCalendar
public GregorianCalendar(Locale aLocale)
GregorianCalendar
based on the current time in the default time zone with the given locale. From source file:Main.java
public static void main(String[] args) { Calendar localTime = Calendar.getInstance(); localTime.set(Calendar.HOUR, 17); localTime.set(Calendar.MINUTE, 15); localTime.set(Calendar.SECOND, 20); int hour = localTime.get(Calendar.HOUR); int minute = localTime.get(Calendar.MINUTE); int second = localTime.get(Calendar.SECOND); System.out.printf("Local time : %02d:%02d:%02d\n", hour, minute, second); Calendar germanyTime = new GregorianCalendar(TimeZone.getTimeZone("Germany")); germanyTime.setTimeInMillis(localTime.getTimeInMillis()); hour = germanyTime.get(Calendar.HOUR); minute = germanyTime.get(Calendar.MINUTE); second = germanyTime.get(Calendar.SECOND); System.out.printf("Germany time: %02d:%02d:%02d\n", hour, minute, second); }
From source file:Main.java
public static long convertTime(long timestamp, String fromTimeZone, String toTimeZone) { Calendar fromCal = new GregorianCalendar(TimeZone.getTimeZone(fromTimeZone)); fromCal.setTimeInMillis(timestamp);//w w w . jav a 2 s . c o m Calendar toCal = new GregorianCalendar(TimeZone.getTimeZone(toTimeZone)); toCal.setTimeInMillis(fromCal.getTimeInMillis()); return toCal.getTimeInMillis(); }
From source file:Main.java
public static long convertTZ(long timestamp, String fromTimeZone, String toTimeZone) { Calendar fromCal = new GregorianCalendar(TimeZone.getTimeZone(fromTimeZone)); fromCal.setTimeInMillis(timestamp);//ww w . jav a 2 s.c om Calendar toCal = new GregorianCalendar(TimeZone.getTimeZone(toTimeZone)); toCal.setTimeInMillis(fromCal.getTimeInMillis()); return toCal.getTimeInMillis(); }
From source file:Main.java
public static String getDateFromUTC(long timestamp) { if (timestamp == 0) timestamp = System.currentTimeMillis(); Date date = new Date(timestamp); Calendar cal = new GregorianCalendar(TimeZone.getTimeZone("GMT")); cal.setTime(date);/* www .java 2 s.c o m*/ String val = (cal.get(Calendar.MONTH) + "/" + cal.get(Calendar.DATE) + " " + cal.get(Calendar.HOUR) + ":" + cal.get(Calendar.MINUTE) + (cal.get(Calendar.AM_PM) == 0 ? "AM" : "PM")); // final Date currentTime = new Date(); // final SimpleDateFormat sdf = new SimpleDateFormat("EEE, MMM d, yyyy hh:mm:ss a z"); // sdf.setTimeZone(TimeZone.getTimeZone("UTC")); // String ret = "UTC time: " + sdf.format(currentTime); // System.out.println(ret); return val; }
From source file:Main.java
public static XMLGregorianCalendar toXMLGregorianCalendarByTimeZone(Date date, TimeZone timeZone) { if (date == null) return null; GregorianCalendar calendar = new GregorianCalendar(timeZone); calendar.setTime(date);/*from w ww . j av a 2 s .c o m*/ return newInstance.newXMLGregorianCalendar(calendar); }
From source file:Main.java
public static Function<Date, XMLGregorianCalendar> fromDate() { return new Function<Date, XMLGregorianCalendar>() { @Override/*from w w w .j a va 2 s .com*/ public XMLGregorianCalendar apply(final Date date) { if (date == null) return null; return new XMLGregorianCalendarImpl(new GregorianCalendar(TimeZone.getDefault()) { { setTime(date); } }); } }; }
From source file:Main.java
public static Calendar getCurrentCalendar() { Calendar currentCal = new GregorianCalendar(Locale.JAPAN); return currentCal; }
From source file:Main.java
public static long convertTime(long time) { GregorianCalendar t1 = new GregorianCalendar(TimeZone.getTimeZone("America/Los_Angeles")); t1.setTimeInMillis(time);//from w w w . java 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
/** * Returns current unix date in GMT as long value * * @return current date as long/*from w w w .jav a 2 s . c om*/ */ public static long getCurrentLongDate() { Calendar cal = new GregorianCalendar(TimeZone.getTimeZone("GMT")); return cal.getTime().getTime(); }
From source file:DateParser.java
private static Calendar getCalendar(String isodate) { // YYYY-MM-DDThh:mm:ss.sTZD StringTokenizer st = new StringTokenizer(isodate, "-T:.+Z", true); Calendar calendar = new GregorianCalendar(TimeZone.getTimeZone("UTC")); calendar.clear();//from w w w . jav a2 s. c o m try { // Year if (st.hasMoreTokens()) { int year = Integer.parseInt(st.nextToken()); calendar.set(Calendar.YEAR, year); } else { return calendar; } // Month if (check(st, "-") && (st.hasMoreTokens())) { int month = Integer.parseInt(st.nextToken()) - 1; calendar.set(Calendar.MONTH, month); } else { return calendar; } // Day if (check(st, "-") && (st.hasMoreTokens())) { int day = Integer.parseInt(st.nextToken()); calendar.set(Calendar.DAY_OF_MONTH, day); } else { return calendar; } // Hour if (check(st, "T") && (st.hasMoreTokens())) { int hour = Integer.parseInt(st.nextToken()); calendar.set(Calendar.HOUR_OF_DAY, hour); } else { calendar.set(Calendar.HOUR_OF_DAY, 0); calendar.set(Calendar.MINUTE, 0); calendar.set(Calendar.SECOND, 0); calendar.set(Calendar.MILLISECOND, 0); return calendar; } // Minutes if (check(st, ":") && (st.hasMoreTokens())) { int minutes = Integer.parseInt(st.nextToken()); calendar.set(Calendar.MINUTE, minutes); } else { calendar.set(Calendar.MINUTE, 0); calendar.set(Calendar.SECOND, 0); calendar.set(Calendar.MILLISECOND, 0); return calendar; } // // Not mandatory now // // Secondes if (!st.hasMoreTokens()) { return calendar; } String tok = st.nextToken(); if (tok.equals(":")) { // secondes if (st.hasMoreTokens()) { int secondes = Integer.parseInt(st.nextToken()); calendar.set(Calendar.SECOND, secondes); if (!st.hasMoreTokens()) { return calendar; } // frac sec tok = st.nextToken(); if (tok.equals(".")) { // bug fixed, thx to Martin Bottcher String nt = st.nextToken(); while (nt.length() < 3) { nt += "0"; } nt = nt.substring(0, 3); // Cut trailing chars.. int millisec = Integer.parseInt(nt); // int millisec = Integer.parseInt(st.nextToken()) * 10; calendar.set(Calendar.MILLISECOND, millisec); if (!st.hasMoreTokens()) { return calendar; } tok = st.nextToken(); } else { calendar.set(Calendar.MILLISECOND, 0); } } else { throw new RuntimeException("No secondes specified"); } } else { calendar.set(Calendar.SECOND, 0); calendar.set(Calendar.MILLISECOND, 0); } // Timezone if (!tok.equals("Z")) { // UTC if (!(tok.equals("+") || tok.equals("-"))) { throw new RuntimeException("only Z, + or - allowed"); } boolean plus = tok.equals("+"); if (!st.hasMoreTokens()) { throw new RuntimeException("Missing hour field"); } int tzhour = Integer.parseInt(st.nextToken()); int tzmin = 0; if (check(st, ":") && (st.hasMoreTokens())) { tzmin = Integer.parseInt(st.nextToken()); } else { throw new RuntimeException("Missing minute field"); } if (plus) { calendar.add(Calendar.HOUR, -tzhour); calendar.add(Calendar.MINUTE, -tzmin); } else { calendar.add(Calendar.HOUR, tzhour); calendar.add(Calendar.MINUTE, tzmin); } } } catch (NumberFormatException ex) { throw new RuntimeException("[" + ex.getMessage() + "] is not an integer"); } return calendar; }