Java Timestamp Parse stringToTimestamp()

Here you can find the source of stringToTimestamp()

Description

string To Timestamp

License

Apache License

Declaration

public static Timestamp stringToTimestamp() 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class Main {

    public static Timestamp stringToTimestamp() {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = df.format(new Date());
        return Timestamp.valueOf(time);
    }//from   ww w.j  av a 2 s  .c o  m

    public static Timestamp stringToTimestamp(String dateStr) {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
        Calendar cal = Calendar.getInstance();
        try {
            Date date = sdf.parse(dateStr);
            date.getTime();
            cal.setTime(date);
            return new Timestamp(cal.getTimeInMillis());
        } catch (ParseException e) {
            e.printStackTrace();
        }

        cal.setTime(new Date());
        return new Timestamp(cal.getTimeInMillis());
    }

    public static Timestamp stringToTimestamp(String dateStr, String pattern) {
        Timestamp dateTime = new Timestamp(System.currentTimeMillis());
        String date = dateStr + " 00:00:00";
        try {
            dateTime = Timestamp.valueOf(date);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dateTime;
    }

    public static Timestamp stringToTimestamp(String dateStr, String HHmmss, String pattern) {
        Timestamp dateTime = new Timestamp(System.currentTimeMillis());
        String date = dateStr + " " + HHmmss;
        try {
            dateTime = Timestamp.valueOf(date);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dateTime;
    }
}

Related

  1. String2Timestamp(String date)
  2. String2Timestamp(String s, String fmt)
  3. string2Timestamp(String strDateTime, String pattern)
  4. String2Timestamp(String strInputDate)
  5. string2Timestamp(String value)
  6. stringToTimestamp(final String dateString)
  7. stringToTimestamp(String aS_Timestamp, String aS_Format, Timestamp aTs_ValidStart, Timestamp aTs_ValidEnd)
  8. stringToTimestamp(String date, String formatStr)
  9. stringToTimeStamp(String dateTimeString, String dateTimeFormat, TimeZone tz, Locale locale)