List of usage examples for java.util TimeZone getTimeZone
public static TimeZone getTimeZone(ZoneId zoneId)
From source file:Main.java
public static String calcUntil(Calendar untilDate, String granularity) { String pattern = ""; if (granularity.equals("YYYY-MM-DDThh:mm:ssZ")) { untilDate.add(Calendar.SECOND, -1); pattern = "yyyy-MM-dd'T'HH:mm:ss'Z'"; } else {/* w w w. ja v a 2 s . c o m*/ untilDate.add(Calendar.DATE, -1); pattern = "yyyy-MM-dd"; } SimpleDateFormat sdf = new SimpleDateFormat(pattern); sdf.setTimeZone(TimeZone.getTimeZone("UTC")); return sdf.format(untilDate.getTime()); }
From source file:Main.java
/** * Formats a date in the xml xs:dateTime format, for mountain time. This * method does not support any other timezone. * * @param calendar the calendar to format * * @return the formatted date//from w w w. jav a 2 s .c o m */ public static String xsDateTimeFormatMountain(final Calendar calendar) { final SimpleDateFormat dateFormat; final SimpleDateFormat timeFormat; final TimeZone currentTimeZone; dateFormat = new SimpleDateFormat("yyyy-MM-dd"); timeFormat = new SimpleDateFormat("HH:mm:ss"); currentTimeZone = TimeZone.getTimeZone("Canada/Mountain"); final long offset; final long hours; final String tzID; offset = currentTimeZone.getOffset(calendar.getTimeInMillis()); hours = -offset / (1000 * 60 * 60); tzID = "-0" + hours + ":00"; // assume single digit hour, we always are return dateFormat.format(calendar.getTime()) + 'T' + timeFormat.format(calendar.getTime()) + tzID; }
From source file:org.traccar.WebDataHandler.java
private static String formatSentence(Position position) { StringBuilder s = new StringBuilder("$GPRMC,"); try (Formatter f = new Formatter(s, Locale.ENGLISH)) { Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("UTC"), Locale.ENGLISH); calendar.setTimeInMillis(position.getFixTime().getTime()); f.format("%1$tH%1$tM%1$tS.%1$tL,A,", calendar); double lat = position.getLatitude(); double lon = position.getLongitude(); f.format("%02d%07.4f,%c,", (int) Math.abs(lat), Math.abs(lat) % 1 * 60, lat < 0 ? 'S' : 'N'); f.format("%03d%07.4f,%c,", (int) Math.abs(lon), Math.abs(lon) % 1 * 60, lon < 0 ? 'W' : 'E'); f.format("%.2f,%.2f,", position.getSpeed(), position.getCourse()); f.format("%1$td%1$tm%1$ty,,", calendar); }/*www . j av a 2 s. c om*/ s.append(Checksum.nmea(s.toString())); return s.toString(); }
From source file:Main.java
public static Date parseTimestamp(String timestamp) { for (SimpleDateFormat format : ACCEPTED_TIMESTAMP_FORMATS) { // TODO: We shouldn't be forcing the time zone when parsing dates. format.setTimeZone(TimeZone.getTimeZone("GMT")); try {/*from w w w. j a va 2 s . co m*/ return format.parse(timestamp); } catch (ParseException ex) { continue; } } // All attempts to parse have failed return null; }
From source file:Main.java
/** * For some reason, can't find this utility method in the java framework. * //from w w w . j a v a2 s.c o m * @param sDateTime * an xsd:dateTime string * @return an equivalent java.util.Date * @throws ParseException */ public static Date parseXsdDateTime(String sDateTime) throws ParseException { final DateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"); int iDotPosition = NORMAL_IDOT_POSITION; if (sDateTime.charAt(0) == '-') { iDotPosition = IDOT_POSITION_IFNEG; } Date result; if (sDateTime.length() <= iDotPosition) { return format.parse(sDateTime + "Z"); } String millis = null; char c = sDateTime.charAt(iDotPosition); if (c == '.') { // if datetime has milliseconds, separate them int eoms = iDotPosition + 1; while (Character.isDigit(sDateTime.charAt(eoms))) { eoms += 1; } millis = sDateTime.substring(iDotPosition, eoms); sDateTime = sDateTime.substring(0, iDotPosition) + sDateTime.substring(eoms); c = sDateTime.charAt(iDotPosition); } if (c == '+' || c == '-') { format.setTimeZone(TimeZone.getTimeZone("GMT" + sDateTime.substring(iDotPosition))); sDateTime = sDateTime.substring(0, iDotPosition) + "Z"; } else if (c != 'Z') { throw new ParseException("Illegal timezone specification.", iDotPosition); } result = format.parse(sDateTime); if (millis != null) { result.setTime(result.getTime() + Math.round(Float.parseFloat(millis) * ONE_SEC_IN_MILLISECS)); } result = offsetDateFromGMT(result); return result; }
From source file:com.alliander.osgp.acceptancetests.ScopedGivWenZenForSlim.java
private static InstantiationStrategy autowireStepDefinitionClassesWithSpring() { rootContext = new AnnotationConfigApplicationContext(); // Force local timezone to UTC (like platform) DateTimeZone.setDefault(DateTimeZone.UTC); TimeZone.setDefault(TimeZone.getTimeZone("UTC")); // Set loglevel to INFO (instead of DEBUG) final Logger root = (Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME); root.setLevel(Level.INFO);/*from w w w.j av a 2s. c o m*/ rootContext.register(TestApplicationContext.class); try { rootContext.refresh(); } catch (final Exception e) { // just for debugging... throw e; } return new SpringInstantiationStrategy(rootContext); }
From source file:Main.java
public static Date parseTimestamp(String timestamp) { for (SimpleDateFormat format : ACCEPTED_TIMESTAMP_FORMATS) { format.setTimeZone(TimeZone.getTimeZone("GMT")); try {//from w ww . j a v a 2 s .c o m return format.parse(timestamp); } catch (ParseException ex) { continue; } } // All attempts to parse have failed return null; }
From source file:io.ignitr.dispatchr.manager.domain.subscription.FindSubscriptionsResponse.java
/** * * @param metadatas// w w w.j ava 2 s .c om * @return */ public static FindSubscriptionsResponse from(List<Subscription> metadatas) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss zzz"); sdf.setTimeZone(TimeZone.getTimeZone("UTC")); FindSubscriptionsResponse response = new FindSubscriptionsResponse(); metadatas.forEach(metadata -> { Result result = new Result(); result.setSubscriptionId(metadata.getId()); result.setClientId(metadata.getClientId()); result.setTopic(metadata.getTopicName()); result.setTopicArn(metadata.getTopicArn()); result.setQueueArn(metadata.getQueueArn()); result.setCreateDate(sdf.format(new Date(metadata.getCreateDate()))); result.setLastSubscribeDate(sdf.format(new Date(metadata.getLastSubscribeDate()))); result.add(linkTo(methodOn(TopicController.class).findOne(metadata.getTopicName(), null)) .withRel("topic")); response.addResult(result); }); return response; }
From source file:net.darkmist.clf.LogParserTest.java
private static Date mkLogTimeAsDate() { GregorianCalendar cal = new GregorianCalendar(2007, 5, 5, 8, 9, 10); cal.setTimeZone(TimeZone.getTimeZone("GMT-1")); return cal.getTime(); }
From source file:net.darkmist.clf.LogEntryTest.java
private static final Date mkStaticDate() { GregorianCalendar cal = new GregorianCalendar(2007, 5, 5, 8, 9, 10); cal.setTimeZone(TimeZone.getTimeZone("GMT-1")); return cal.getTime(); }