Java Utililty Methods String to Timestamp

List of utility methods to do String to Timestamp

Description

The list of methods to do String to Timestamp are organized into topic(s).

Method

longgetTimestamp(String timezone, String dateTime, String pattern)
get Timestamp
SimpleDateFormat sdf = null;
if (null == pattern || "".equals(pattern)) {
    sdf = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS);
} else {
    sdf = new SimpleDateFormat(pattern);
sdf.setTimeZone(TimeZone.getTimeZone(timezone));
long b = sdf.parse(dateTime).getTime() / 1000;
...
longgetTimestampFromDateString(String date)
This method computes the timestamp of the beginning of the day (UTC), given a random string yyyy-MM-dd
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
cal.setTime(sdf.parse(date));
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
return (cal.getTimeInMillis() / 1000L);
...
longgetTimeStampInSecond(String timeStr)
get Time Stamp In Second
SimpleDateFormat timeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
    return timeFormat.parse(timeStr).getTime() / 1000;
} catch (ParseException e) {
    e.printStackTrace();
    return -1;
booleanisTimestamp(String str, boolean allowEmpty)
is Timestamp
if (allowEmpty && isEmpty(str)) {
    return true;
if (str.length() != 19 || strToDate(str) == null) {
    return false;
return true;
longisTimeStampValid(String timestamp)
This method validates the given time stamp in String format
boolean time = false;
try {
    StringTokenizer st = new StringTokenizer(timestamp, " ");
    if (st.countTokens() != 2) {
        return 0;
    String[] dateAndTime = new String[2];
    int i = 0;
...
longstring2Timestamp(String dateString, String timezone)
string Timestamp
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
if ((timezone != null) && (!timezone.equals(""))) {
    df.setTimeZone(TimeZone.getTimeZone(timezone));
Date date1 = df.parse(dateString);
long temp = date1.getTime();
return temp;
longstring2timestamp(String timeString)
stringtimestamp
int decimalIndex = timeString.indexOf(".") + 1;
String formatString;
if (decimalIndex > 0) {
    formatString = timeString.substring(0, decimalIndex);
    int cnt = 0;
    while (decimalIndex < timeString.length() - 1 && cnt < 3) {
        formatString += timeString.substring(decimalIndex, decimalIndex + 1);
        decimalIndex++;
...
longstringToTimestamp(String date)
string To Timestamp
long time = -1L;
if (date.indexOf(" ") == -1) {
    try {
        time = (new SimpleDateFormat("yyyy-MM-dd")).parse(date).getTime() / 1000;
    } catch (ParseException pe) {
} else {
    try {
...
Stringtimestamp(String string)
Prepend a timestamp to the string defined by parameter string
String formattedString = DateFormat.getDateTimeInstance().format(Calendar.getInstance().getTime()) + "    "
        + string;
return formattedString;
StringtimeStamp2Date(String timestampString)
time Stamp Date
Long timestamp = Long.parseLong(timestampString) * 1000;
String date = new java.text.SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(new java.util.Date(timestamp));
return date;