Java String to Timestamp isTimestamp(String str, boolean allowEmpty)

Here you can find the source of isTimestamp(String str, boolean allowEmpty)

Description

is Timestamp

License

Open Source License

Declaration

public static boolean isTimestamp(String str, boolean allowEmpty) 

Method Source Code


//package com.java2s;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Main {

    public static boolean isTimestamp(String str, boolean allowEmpty) {
        if (allowEmpty && isEmpty(str)) {
            return true;
        }//w  w  w .  ja va2s  .co  m
        if (str.length() != 19 || strToDate(str) == null) {
            return false;
        }

        return true;
    }

    public static boolean isEmpty(String str) {
        return ((str == null) || (str.length() == 0));
    }

    public static Date strToDate(String str) {
        if (isEmpty(str)) {
            return null;
        }
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            if (str.length() != 19) {
                return null;
            }
            return dateFormat.parse(str);
        } catch (ParseException e) {
            return null;
        }
    }
}

Related

  1. getTimeStamp(String style, Date date)
  2. getTimestamp(String timeStampStr, String logRundateIdStr)
  3. getTimestamp(String timezone, String dateTime, String pattern)
  4. getTimestampFromDateString(String date)
  5. getTimeStampInSecond(String timeStr)
  6. isTimeStampValid(String timestamp)
  7. string2Timestamp(String dateString, String timezone)
  8. string2timestamp(String timeString)
  9. stringToTimestamp(String date)