@Override public long valueToCode(String value) { long millis = DateFormat.stringToMillis(value); return millis / 1000; }
public static String formatToDateStr(long millis) { return formatToDateStr(millis, DEFAULT_DATE_PATTERN); }
case 1: normalized = TimeUtil.getMinuteStart(t); result.add(DateFormat.formatToTimeWithoutMilliStr(normalized)); break; case 2: normalized = TimeUtil.getHourStart(t); result.add(DateFormat.formatToTimeWithoutMilliStr(normalized)); break; case 3: normalized = TimeUtil.getDayStart(t); result.add(DateFormat.formatToDateStr(normalized)); break; case 4: normalized = TimeUtil.getWeekStart(t); result.add(DateFormat.formatToDateStr(normalized)); break; case 5: normalized = TimeUtil.getMonthStart(t); result.add(DateFormat.formatToDateStr(normalized)); break; case 6: normalized = TimeUtil.getQuarterStart(t); result.add(DateFormat.formatToDateStr(normalized)); break; case 7: normalized = TimeUtil.getYearStart(t); result.add(DateFormat.formatToDateStr(normalized)); break; default:
private static String datePlus(String date, int plusDays) { long millis = DateFormat.stringToMillis(date); millis += (1000L * 3600L * 24L) * plusDays; return DateFormat.formatToDateStr(millis); }
public static boolean isInputFormatDate(String dateStr, String formatStr) { try { return dateStr.equals(dateToString(stringToDate(dateStr, formatStr), formatStr)); } catch (Exception ex) { return false; } }
public static long stringToMillis(String str) { // try to be smart and guess the date format if (isAllDigits(str)) { if (str.length() == 8 && isInputFormatDate(str, COMPACT_DATE_PATTERN)) //TODO: might be prolematic if an actual ts happends to be 8 digits, e.g. 1970-01-01 10:00:01.123 return stringToDate(str, COMPACT_DATE_PATTERN).getTime(); else if (str.length() == 10 && isInputFormatDate(str, YYYYMMDDHH)) return stringToDate(str, YYYYMMDDHH).getTime(); else if (str.length() == 12 && isInputFormatDate(str, YYYYMMDDHHMM)) return stringToDate(str, YYYYMMDDHHMM).getTime(); else if (str.length() == 14 && isInputFormatDate(str, YYYYMMDDHHMMSS)) return stringToDate(str, YYYYMMDDHHMMSS).getTime(); else return Long.parseLong(str); } else if (str.length() == 10) { return stringToDate(str, DEFAULT_DATE_PATTERN).getTime(); } else if (str.length() == 13) { return stringToDate(str, YYYY_MM_DD_HH).getTime(); } else if (str.length() == 16) { return stringToDate(str, YYYY_MM_DD_HH_MM).getTime(); } else if (str.length() == 19) { return stringToDate(str, DEFAULT_DATETIME_PATTERN_WITHOUT_MILLISECONDS).getTime(); } else if (str.length() > 19) { return stringToDate(str, DEFAULT_DATETIME_PATTERN_WITH_MILLISECONDS).getTime(); } else { throw new IllegalArgumentException("there is no valid date pattern for:" + str); } }
public void encodeDecode(String origin) { long a = encode(origin); String back = decode(a); String originChoppingMilis = DateFormat.formatToTimeWithoutMilliStr(DateFormat.stringToMillis(origin)); String backMillis = DateFormat.formatToTimeWithoutMilliStr(Long.parseLong(back)); Assert.assertEquals(originChoppingMilis, backMillis); }
public void test1() throws Exception { System.out.println(org.apache.kylin.common.util.DateFormat.formatToTimeStr(1433833611000L)); System.out.println(org.apache.kylin.common.util.DateFormat.formatToTimeStr(1433250517000L)); System.out.println(org.apache.kylin.common.util.DateFormat.stringToMillis("2015-06-01 00:00:00")); System.out.println(org.apache.kylin.common.util.DateFormat.stringToMillis("2015-05-15 17:00:00"));
Date dateValue = DateFormat.stringToDate(strValue); // NOTE: forces GMT timezone long millis = dateValue.getTime(); long days = millis / (1000 * 3600 * 24); return new BigDecimal(strValue); } else if ("timestamp".equals(dataType)) { return Long.valueOf(DateFormat.stringToMillis(strValue)); } else if ("float".equals(dataType)) { return Float.valueOf(strValue);
private String formatTime(String dateStr, DataType dataType) { if (dataType.isDatetime() || dataType.isTime()) { throw new RuntimeException("Datetime and time type are not supported yet"); } if (DateFormat.isSupportedDateFormat(dateStr)) { return dateStr; } long millis = Long.parseLong(dateStr); if (dataType.isTimestamp()) { return DateFormat.formatToTimeStr(millis); } else if (dataType.isDate()) { return DateFormat.formatToDateStr(millis); } else { throw new RuntimeException("Unknown type " + dataType + " to formatTime"); } } }
@Override public String next() { if (type.isStringFamily()) { // string return format.replace(ColumnGenConfig.$RANDOM, "" + randomInt()); } else if (type.isTimeFamily()) { // time return DateFormat.formatToTimeStr(randomMillis(), format); } else if (type.isDateTimeFamily()) { // date return DateFormat.formatToDateStr(randomMillis(), format); } else if (type.isIntegerFamily()) { // integer return formatNumber(randomInt()); } else if (type.isNumberFamily()) { // double return formatNumber(randomDouble()); } else { throw new IllegalStateException(); } }
public DateTimeParser(Map<String, String> properties) { super(properties); tsPattern = properties.get(StreamingParser.PROPERTY_TS_PATTERN); try { formatter = org.apache.kylin.common.util.DateFormat.getDateFormat(tsPattern); } catch (Throwable e) { throw new IllegalStateException("Invalid tsPattern: '" + tsPattern + "'."); } }
public static Date stringToDate(String str) { return stringToDate(str, DEFAULT_DATE_PATTERN); }
@Override public String toString() { return "BadQueryEntry [ adj=" + adj + ", server=" + server + ", startTime=" + DateFormat.formatToTimeStr(startTime) + " ]"; } }
@Override final protected String getValueFromIdImpl(int id) { if (id < baseId || id > maxId) throw new IllegalArgumentException("ID '" + id + "' is out of range [" + baseId + "," + maxId + "]"); long millis = DateDimEnc.getMillisFromNumOfDaysSince0000(calcSeqNoFromId(id)); return dateToString(new Date(millis), pattern); }
public static long stringToMillis(String str) { if (isAllDigits(str)) { return Long.parseLong(str); } else if (str.length() == 10) { return stringToDate(str, DEFAULT_DATE_PATTERN).getTime(); } else if (str.length() == 19) { return stringToDate(str, DEFAULT_DATETIME_PATTERN_WITHOUT_MILLISECONDS).getTime(); } else if (str.length() == 23) { return stringToDate(str, DEFAULT_DATETIME_PATTERN_WITH_MILLISECONDS).getTime(); } else { throw new IllegalArgumentException("there is no valid date pattern for:" + str); } }
/** * * @param id * @return return like "0000001430812800000" */ @Override protected String getValueFromIdImpl(int id) { if (id == nullId()) return null; long millis = 1000L * id; return DateFormat.formatToTimeWithoutMilliStr(millis); }
private static String datePlus(String date, int plusDays) { long millis = DateFormat.stringToMillis(date); millis += (1000L * 3600L * 24L) * plusDays; return DateFormat.formatToDateStr(millis); }
public void encodeDecode(String origin) { int a = dict.getIdFromValue(origin); String back = dict.getValueFromId(a); String originChoppingMilis = DateFormat.formatToTimeWithoutMilliStr(DateFormat.stringToMillis(origin)); Assert.assertEquals(originChoppingMilis, back); }
private String formatTime(String dateStr, DataType dataType) { if (dataType.isDatetime() || dataType.isTime()) { throw new RuntimeException("Datetime and time type are not supported yet"); } if (DateFormat.isSupportedDateFormat(dateStr)) { return dateStr; } long millis = Long.parseLong(dateStr); if (dataType.isTimestamp()) { return DateFormat.formatToTimeStr(millis); } else if (dataType.isDate()) { return DateFormat.formatToDateStr(millis); } else { throw new RuntimeException("Unknown type " + dataType + " to formatTime"); } } }