Convert string Date into timestamp in Android?

后端 未结 5 728
面向向阳花
面向向阳花 2020-12-01 12:03

I want to convert my date (which is in String format), e.g. 13-09-2011, into Timestamp. I used below code but I got the 2011-09-13 00:00:00.0 as a result. But I want Timest

5条回答
  •  栀梦
    栀梦 (楼主)
    2020-12-01 12:50

    This line:

    "Today is " +timeStampDate
    

    calls TimeStamp.toString() method "which Formats a timestamp in JDBC timestamp escape format. yyyy-mm-dd hh:mm:ss.fffffffff, where ffffffffff indicates nanoseconds."

    The TimeStamp you got internally has the value you want. If you want to get it than use: System.out.println("Today is " + timeStampDate.getTime());

        String str_date="13-09-2011";
        DateFormat formatter ; 
        Date date ; 
        formatter = new SimpleDateFormat("dd-MM-yyyy");
        date = (Date)formatter.parse(str_date);
        java.sql.Timestamp timeStampDate = new Timestamp(date.getTime());
        System.out.println("Today is " + timeStampDate.getTime());
    

    Or if you don't need the Timestamp, you can directly use date.getTime(). It "Returns the Date as a millisecond value.":

        String str_date="13-09-2011";
        DateFormat formatter ; 
        Date date ; 
        formatter = new SimpleDateFormat("dd-MM-yyyy");
        date = (Date)formatter.parse(str_date);
        System.out.println("Today is " + date.getTime());
    

提交回复
热议问题