Here you can find the source of parseDateTimeString(final String date)
Parameter | Description |
---|---|
date | The Z time string date to parse in 'normal' of 'file' formats. |
public static Long parseDateTimeString(final String date) throws ParseException
//package com.java2s; /*//from ww w. j ava2s. c o m * Copyright 2016 Crown Copyright * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Locale; import java.util.TimeZone; public class Main { private static final TimeZone UTC_TIME_ZONE = TimeZone.getTimeZone("+0000"); private static final String FILE_MILLISECOND_SEPERATOR = ","; private static final String NULL = "NULL"; /** * @param date * The Z time string date to parse in 'normal' of 'file' formats. * @return A Date object set to the supplied date. */ public static Long parseDateTimeString(final String date) throws ParseException { if (NULL.equals(date) || date == null || date.length() == 0) { return null; } String tmp = date.replace('T', ' '); tmp = tmp.replaceAll("#", ":"); tmp = tmp.replaceAll(FILE_MILLISECOND_SEPERATOR, "."); tmp = tmp.replace("Z", " +0000"); final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS zzz"); final Calendar cal = Calendar.getInstance(UTC_TIME_ZONE, Locale.ROOT); sdf.setCalendar(cal); sdf.parse(tmp); return cal.getTime().getTime(); } }