Here you can find the source of convertDate(String value)
Parameter | Description |
---|---|
value | a parameter |
public static long convertDate(String value)
//package com.java2s; /*//from w w w . j av a 2 s .c om * #%L * Util.java - method51 - University of Sussex - 2,013 * %% * Copyright (C) 2013 - 2014 University of Sussex * %% * 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. * #L% */ import java.text.ParseException; import java.text.SimpleDateFormat; public class Main { public static final String dateTimeFormat = "yy-MM-dd HH:mm:ss"; public static final String dateTimeFormat2 = "yy-MM-dd-HH-mm-ss"; /** * Convert from the two commonly used date formats to a timestamp * @param value * @return */ public static long convertDate(String value) { long timestamp; try { timestamp = Long.parseLong(value); if (timestamp < 0) { throw new NumberFormatException("negative timestamp!"); } } catch (NumberFormatException e) { try { timestamp = new SimpleDateFormat(dateTimeFormat).parse(value).getTime(); } catch (ParseException e1) { try { timestamp = new SimpleDateFormat(dateTimeFormat2).parse(value).getTime(); } catch (ParseException e2) { throw new RuntimeException(e2.getMessage() + " could not parse time string " + value); } } } return timestamp; } }