public static void testTimeZone() { System.out.println("演示时区"); String format = "yyyy-MM-dd HH:mm:ss zZZ"; // DateTime的毫秒即System的毫秒,即1970到现在的UTC的毫秒数. System.out.println(new DateTime().getMillis() + " " + System.currentTimeMillis()); // 将日期按默认时区打印 DateTime fooDate = new DateTime(1978, 6, 1, 12, 10, 8, 0); System.out.println( fooDate.toString(format) + " " + fooDate.getMillis()); // "1978-06-01 12:10:08" // 将日期按UTC时区打印 DateTime zoneWithUTC = fooDate.withZone(DateTimeZone.UTC); System.out.println( zoneWithUTC.toString(format) + " " + zoneWithUTC.getMillis()); // "1978-06-01 04:10:08", sameMills // 按不同的时区分析字符串,得到不同的时间 String dateString = "1978-06-01 12:10:08"; DateTimeFormatter fmt = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss"); DateTime parserResult1 = fmt.withZone(DateTimeZone.forID("US/Pacific")).parseDateTime(dateString); DateTime parserResult2 = fmt.withZone(DateTimeZone.UTC).parseDateTime(dateString); System.out.println(parserResult1.toString(format) + " " + parserResult1.getMillis()); System.out.println(parserResult2.toString(format) + " " + parserResult2.getMillis()); }
/** * CSV reader that waits for a 2 columns csv files with or without a header. If less than 2 * columns ==> exception, otherwise, the 3rd and following columns are ignored * * @param in * @param hasHeader * @param inputDatetimeFormat input date format * @return * @throws IOException * @throws IllegalArgumentException * @throws ArrayIndexOutOfBoundsException */ public static List<Record> load( Reader in, boolean hasHeader, DateTimeFormatter inputDatetimeFormat) throws IOException { List<Record> records = new ArrayList<>(); for (CSVRecord record : CSVFormat.DEFAULT.parse(in)) { try { if (!hasHeader) { StandardRecord event = new StandardRecord("sensors"); event.setField( TIMESTAMP_KEY, FieldType.LONG, inputDatetimeFormat .withZone(DateTimeZone.UTC) .parseDateTime(record.get(0)) .getMillis()); event.setField(VALUE_KEY, FieldType.DOUBLE, Double.parseDouble(record.get(1))); records.add(event); } else { TIMESTAMP_KEY = record.get(0); VALUE_KEY = record.get(1); } hasHeader = false; } catch (Exception e) { logger.error("Parsing error " + e.getMessage()); throw new RuntimeException("parsing error", e); } } return records; }
private long parseDateTime(String value, DateTimeZone timeZone) { // first check for timestamp if (value.length() > 4 && StringUtils.isNumeric(value)) { try { long time = Long.parseLong(value); return timeUnit.toMillis(time); } catch (NumberFormatException e) { throw new ElasticsearchParseException( "failed to parse date field [" + value + "] as timestamp", e); } } DateTimeFormatter parser = dateTimeFormatter.parser(); if (timeZone != null) { parser = parser.withZone(timeZone); } try { return parser.parseMillis(value); } catch (IllegalArgumentException e) { throw new ElasticsearchParseException( "failed to parse date field [" + value + "] with format [" + dateTimeFormatter.format() + "]", e); } }
/** * @param when The instant * @param locale The required locale * @return The instant formatted as "ddd, MMM dd" (Saturday, January 01) in the system timezone */ public static String formatTransactionDateLocal(ReadableInstant when, Locale locale) { if (when == null) { return ""; } return utcTransactionDateFormatter .withZone(DateTimeZone.getDefault()) .withLocale(locale) .print(when); }
private Optional<DateTime> parseStartTime(SportsMatchData match, OptaSportType sport) { String dateStr = match.matchInformation().date().date(); Optional<DateTimeZone> timeZone = utility.fetchTimeZone(sport); if (!timeZone.isPresent()) { log.error("No time zone mapping found for sport {}", sport); return Optional.absent(); } return Optional.of(DATE_TIME_FORMATTER.withZone(timeZone.get()).parseDateTime(dateStr)); }
/** * Sets the time zone to use for formatting the time. * * @param zone time zone to use * @return self, for chaining */ public TimeFormatter withZone(DateTimeZone zone) { dtf = dtf.withZone(zone); return this; }
/** * @param when The instant * @param locale The required locale * @return The instant formatted as ISO8601 e.g. "2000-01-02T03:04:05" in the system timezone */ public static String formatIso8601Local(ReadableInstant when, Locale locale) { if (when == null) { return ""; } return utcIso8601.withZone(DateTimeZone.getDefault()).withLocale(locale).print(when); }
/** * @param when The instant * @return The instant formatted for HTTP as defined in RFC 1123 e.g. "Sat, 01 Jan 2000 23:59:59 * GMT" in UTC */ public static String formatHttpDateHeaderLocal(ReadableInstant when) { if (when == null) { return ""; } return utcHttpDateFormatter.withZone(DateTimeZone.getDefault()).print(when); }
/** * @param when The instant * @return The instant formatted as "yyyy-MM-dd" in the system timezone */ public static String formatCompactDateWithHyphensLocal(ReadableInstant when) { if (when == null) { return ""; } return utcShortDateWithHyphensFormatter.withZone(DateTimeZone.getDefault()).print(when); }
public static String format(DateTime toFormat, TimeZone tz) { return LOCAL_SUB_SECOND_FMT.withZone(DateTimeZone.forTimeZone(tz)).print(toFormat.getMillis()); }
public static long parseHiveTimestamp(String value, DateTimeZone timeZone) { return HIVE_TIMESTAMP_PARSER.withZone(timeZone).parseMillis(value); }
public static String formatarComTimezone(Date date, DateTimeZone timezone) { DateTimeFormatter dtf = ISODateTimeFormat.dateTimeNoMillis(); DateTime d = new DateTime(date); return d.toString(dtf.withZone(timezone)); }
public static Date dataHora(String dataHora, DateTimeZone timezone) { DateTimeFormatter dtf = DateTimeFormat.forPattern("dd/MM/yy HH:mm:ss"); DateTime dateTime = dtf.withZone(timezone).parseDateTime(dataHora); return dateTime.toDate(); }