List of usage examples for java.util Calendar set
public final void set(int year, int month, int date, int hourOfDay, int minute, int second)
YEAR
, MONTH
, DAY_OF_MONTH
, HOUR_OF_DAY
, MINUTE
, and SECOND
. From source file:mojo.core.test.BaseTest.java
protected static Date createDateTime(int year, int month, int day, int hour, int minute, int second) { Calendar calendar = GregorianCalendar.getInstance(); calendar.set(year, month, day, hour, minute, second); calendar.set(Calendar.MILLISECOND, 0); return calendar.getTime(); }
From source file:Main.java
public static Calendar dateToCalendar(String strYear, String strMonth, String strDay, boolean b) { int year, month, day; try {/*from www . j av a2s . c om*/ year = Integer.parseInt(strYear); month = Integer.parseInt(strMonth) - 1; day = Integer.parseInt(strDay); } catch (StringIndexOutOfBoundsException e) { return null; } catch (NumberFormatException e) { return null; } Calendar cal = Calendar.getInstance(TimeZone.getDefault()); cal.setLenient(b); cal.set(year, month, day, 0, 0, 0); cal.set(Calendar.MILLISECOND, 0); return cal; }
From source file:Main.java
/** * Get a Calendar for this gdate, or ndef in case of failure * @param gdate//from ww w . j av a 2 s. c om * @param def * @return */ public static Calendar getCalendar(String gdate, Calendar def) { if ((gdate != null) && (gdate.length() < 14)) gdate = gdate + "00000000000000"; if ((gdate == null) || (gdate.length() < 14)) { return def; } Calendar date = CalendargetInstance(); try { date.set(Integer.parseInt(gdate.substring(0, 4)), Integer.parseInt(gdate.substring(4, 6)) - 1, Integer.parseInt(gdate.substring(6, 8)), Integer.parseInt(gdate.substring(8, 10)), Integer.parseInt(gdate.substring(10, 12)), Integer.parseInt(gdate.substring(12, 14))); date.set(Calendar.MILLISECOND, Integer.parseInt(gdate.substring(15, 18))); } catch (Exception e) { return def; } //System.out.println("getCalendar "+gdate+" into "+date); return date; }
From source file:XSDDateTime.java
public static Calendar parse(String dt) { String[] dateTime = dt.split("T"); String date = dateTime[0];/*from www.j a v a2 s . co m*/ String time = dateTime[1]; String[] ymd = date.split("-"); int year = Integer.parseInt(ymd[0]); int month = Integer.parseInt(ymd[1]) - 1; int day = Integer.parseInt(ymd[2]); String[] hms = time.split(":"); int hour = Integer.parseInt(hms[0]); int minutes = Integer.parseInt(hms[1]); int seconds = Integer.parseInt(hms[2].substring(0, 2)); TimeZone tz = TimeZone.getTimeZone("GMT+00:00"); Calendar cal = Calendar.getInstance(tz, Locale.US); cal.set(year, month, day, hour, minutes, seconds); return cal; }
From source file:Main.java
/** * Converts an XMLGregorianCalendar to a Date. * * @param xmlDate/*ww w. j av a 2 s . co m*/ * XMLGregorianCalendar to convert. * @return corresponding date object. */ public static Date getDate(final XMLGregorianCalendar xmlDate) { // TODO: is this equivalent to getDate(String) processing above?? // start with UTC, i.e. no daylight savings time. TimeZone timezone = TimeZone.getTimeZone("GMT"); // adjust timezone to match xmldate int offsetMinutes = xmlDate.getTimezone(); if (offsetMinutes != DatatypeConstants.FIELD_UNDEFINED) { timezone.setRawOffset( // convert minutes to milliseconds offsetMinutes * 60 // seconds per minute * 1000 // milliseconds per second ); } // use calendar so parsed date will be UTC Calendar calendar = Calendar.getInstance(timezone); calendar.clear(); calendar.set(xmlDate.getYear(), // xmlcalendar is 1 based, calender is 0 based xmlDate.getMonth() - 1, xmlDate.getDay(), xmlDate.getHour(), xmlDate.getMinute(), xmlDate.getSecond()); Date date = calendar.getTime(); int millis = xmlDate.getMillisecond(); if (millis != DatatypeConstants.FIELD_UNDEFINED) { calendar.setTimeInMillis(calendar.getTimeInMillis() + millis); } return date; }
From source file:erpsystem.chart.Charts.java
private static Calendar copyCalendar(Calendar calendar) { Calendar result = Calendar.getInstance(); result.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), calendar.get(Calendar.SECOND)); return result; }
From source file:gov.wa.wsdot.cms.utils.Migration.java
/** * Calculate number of days since the 1900 epoch. * // w ww . j ava 2 s .co m * @param daysSinceEpoch days since epoch in milliseconds * @return converted date in the form "Tue Apr 09 07:56:37 PDT 2013" */ public static String convertDays(double daysSinceEpoch) { DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd kk:mm:ss"); dateFormat.setTimeZone(TimeZone.getTimeZone("GMT")); // Set TimeZone to UTC Calendar startDate = Calendar.getInstance(); startDate.set(1899, 11, 31, 16, 0, 0); long MILLIS_PER_DAY = 86400000; long createdWhen = (long) (daysSinceEpoch * MILLIS_PER_DAY + startDate.getTimeInMillis()) - 2 * MILLIS_PER_DAY; return dateFormat.format(new Date(createdWhen)); }
From source file:Main.java
private static GregorianCalendar gmtStringToGregorianCalendar(String stringTypeDate) throws DatatypeConfigurationException { String yyyy = stringTypeDate.substring(0, 4); String mm = stringTypeDate.substring(5, 7); String dd = stringTypeDate.substring(8, 10); String hh = "00"; String mi = "00"; String ss = "00"; String ms = "00"; String tz = null;/* w ww .ja va2 s . c o m*/ if (stringTypeDate.length() > 23) { hh = stringTypeDate.substring(11, 13); mi = stringTypeDate.substring(14, 16); ss = stringTypeDate.substring(17, 19); ms = stringTypeDate.substring(20, 23); tz = stringTypeDate.substring(23); } else { tz = stringTypeDate.substring(10); //tz = "+09:00"; } if (tz.equals("Z")) { tz = "UTC"; } else { tz = "GMT" + tz; } int iyyyy = Integer.parseInt(yyyy); int imm = Integer.parseInt(mm) - 1; int idd = Integer.parseInt(dd); int ihh = Integer.parseInt(hh); int imi = Integer.parseInt(mi); int iss = Integer.parseInt(ss); int ims = Integer.parseInt(ms); Calendar c = Calendar.getInstance(); c.setTimeZone(TimeZone.getTimeZone(tz)); c.set(iyyyy, imm, idd, ihh, imi, iss); c.set(Calendar.MILLISECOND, ims); GregorianCalendar cal = new GregorianCalendar(); cal.setTime(c.getTime()); cal.setTimeZone(TimeZone.getTimeZone("ROK")); return cal; }
From source file:Main.java
/** * Date from database as string can convert to calendar * @param date A formated (yyyy-MM-dd HH:mm:ss) date from database * @return A converted calendar object// ww w.j a va 2s . com */ public static Calendar stringToCalendar(String date) { Calendar calendar = Calendar.getInstance(); String[] dateTime = date.split(" "); String[] s_date = dateTime[0].split("-"); String[] s_time = dateTime[1].split(":"); calendar.set(toInt(s_date[0]), (toInt(s_date[1]) - 1), toInt(s_date[2]), toInt(s_time[0]), toInt(s_time[1]), toInt(s_time[2])); return calendar; }
From source file:com.collabnet.ccf.core.utils.DateUtil.java
public static Date convertGMTToTimezoneAbsoluteDate(Date dateValue, String sourceSystemTimezone) { Calendar cal = new GregorianCalendar(TimeZone.getTimeZone(GMT_TIME_ZONE_STRING)); cal.setLenient(false);/*from w w w . ja v a 2 s . c om*/ cal.setTime(dateValue); Calendar newCal = new GregorianCalendar(TimeZone.getTimeZone(sourceSystemTimezone)); newCal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0); newCal.set(Calendar.MILLISECOND, 0); Date returnDate = newCal.getTime(); return returnDate; }