Java Date Format Change convertDate(String value)

Here you can find the source of convertDate(String value)

Description

Convert from the two commonly used date formats to a timestamp

License

Apache License

Parameter

Parameter Description
value a parameter

Declaration

public static long convertDate(String value) 

Method Source Code

//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;
    }
}

Related

  1. convertDate(String date, String[] formats)
  2. convertDate(String dateIn, String fromDateFormat, String toDateFormat)
  3. convertDate(String inPattern, String outPattern, String date)
  4. convertDate(String inPattern, String outPattern, String date)
  5. convertDate(String str)
  6. convertDateFormat(String date, SimpleDateFormat formatBefore, SimpleDateFormat formatAfter)
  7. convertDateFormat(String sourceDate, String sourceDateFormat, String destDateFormat)
  8. convertDateFormatByRawOffset(DateFormat formatter, int rawOffset, long milliSecond)
  9. convertDateFromInDayFormat(String stringDate, String hhmmss)