Java Timestamp Parse parseTimestamp(String timestamp)

Here you can find the source of parseTimestamp(String timestamp)

Description

parse Timestamp

License

Open Source License

Declaration

public static Date parseTimestamp(String timestamp) throws ParseException 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.TimeZone;

public class Main {
    private static final TimeZone UTC = TimeZone.getTimeZone("UTC");
    private static final String DATE_TIME_FORMAT = "yyyy-MM-dd'T'HH:mm:ss'Z'";
    private static final String DATE_FORMAT = "yyyy-MM-dd";
    private static final int LONG_FORMAT_LENGTH = DATE_TIME_FORMAT.replace("'", "").length();
    private static final int SHORT_FORMAT_LENGTH = DATE_FORMAT.replace("'", "").length();

    public static Date parseTimestamp(String timestamp) throws ParseException {
        int length = timestamp.length();

        DateFormat format = null;
        if (length == LONG_FORMAT_LENGTH) {
            format = new SimpleDateFormat(DATE_TIME_FORMAT);
        } else if (length == SHORT_FORMAT_LENGTH) {
            format = new SimpleDateFormat(DATE_FORMAT);
        } else {/*from ww w  .j  a v  a  2s.  c  o m*/
            throw new ParseException("timestamp has unexpected format: '" + timestamp + "'", 0);
        }

        format.setCalendar(new GregorianCalendar(UTC));
        return format.parse(timestamp);
    }
}

Related

  1. parseTimestamp(String time)
  2. parseTimestamp(String timestamp)
  3. parseTimeStamp(String timeStamp)
  4. parseTimeStamp(String timeStamp)
  5. parseTimestamp(String timestamp)
  6. parseTimestamp(String timestamp, String dateFormat, Locale locale)
  7. parseTimeStamp(String timeStampString)
  8. parseTimestamp(String value)
  9. parseTimestamp(String value)