1

Recently I'm having a problem with Timestamp and HTML input type Date:

This is my HTML/JSP:

<div class="form-group">
   <label>Your day of birth</label>
   <input class="form-control form-control-lg" type="date" name="txtBirthdate" required="">
</div>

This is my Java Servlet:

String birth = request.getParameter(Constants.BIRTHDATE_TXT);
System.out.println(birth);
Timestamp bDate = new Timestamp(((new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(birth)).getTime()));
System.out.println(bDate);
Timestamp joinDate = new Timestamp(Calendar.getInstance().getTime().getTime());

I cannot parsing the String birth into Timestamp, are there any ways for converting it? And also am I right when you pare the yyyy-MM-dd string using the SimpleDateFormat, it will set the HH:mm:ss part with default value is 00:00:0000?

Thank you for your help

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
Jason
  • 13
  • 4
  • I recommend you neither use `Timestamp`, `SimpleDateFormat` nor `Calendar`. All of those classes are poorly designed and long outdated, `SimpleDateFormat` in particular notoriously troublesome. Instead use for example `LocalDateTime` and `DateTimeFormatter`, both from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Dec 28 '20 at 06:00
  • 1
    We are missing some information here: What is an example of the string you are trying to parse? What error message are you getting? Or which incorrect result? And what were you thinking that you needed a `Timestamp` for? – Ole V.V. Dec 28 '20 at 06:02
  • Were you serious about the jodatime tag? Did you want to use Joda-Time? – Ole V.V. Dec 28 '20 at 08:04
  • @OleV.V. Thank you for your suggestion. My problem is I don't really know how to parse date like example: "2020-12-28" into Timestamp. I'll try to use LocalDateTime for this. – Jason Dec 28 '20 at 08:35
  • Use `LocalDate`, `LocalDate.parse("2020-12-28")` works without any formatter. And as I said, you most probably neither want nor need `Timestamp`. What exactly you do need instead, I don’t know since it depends on circumstances you haven’t yet told us. – Ole V.V. Dec 28 '20 at 09:20

1 Answers1

1

The date-time API of java.util and their formatting API, SimpleDateFormat are outdated and error-prone. Note that java.sql.Timestamp has inherited the same drawbacks as it extends java.util.Date. It is recommended to stop using them completely and switch to the modern date-time API. For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.

You have mentioned,

My problem is I don't really know how to parse date like example: "2020-12-28" into Timestamp

You have also mentioned,

And also am I right when you pare the yyyy-MM-dd string using the SimpleDateFormat, it will set the HH:mm:ss part with default value is 00:00:0000?

From these two requirements, I infer that you need a date e.g. 2020-12-28 combined with the time e.g. 00:00:00 which is nothing but the start of the day. java.time provides a clean API, LocalDate#atStartOfDay to achieve this.

Demo:

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

public class Main {
    public static void main(String[] args) {
        String strDate = "2020-12-28";

        // Parse the given date string into LocalDate. Note that you do not need a
        // DateTimeFormatter to parse it as it is already in ISO 8601 format
        LocalDate date = LocalDate.parse(strDate);

        // Note: In the following line, replace ZoneId.systemDefault() with the required
        // Zone ID which specified in the format, Continent/City e.g.
        // ZoneId.of("Europe/London")
        ZonedDateTime zdt = date.atStartOfDay(ZoneId.systemDefault());

        // Print the default format i.e. the value of zdt#toString. Note that the
        // default format omits seconds and next smaller units if seconds part is zero
        System.out.println(zdt);

        // Get and print just the date-time without timezone information
        LocalDateTime ldt = zdt.toLocalDateTime();
        System.out.println(ldt);

        // Get and print zdt in a custom format
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm:ss.SSS", Locale.ENGLISH);
        String formatted = zdt.format(dtf);
        System.out.println(formatted);
    }
}

Output:

2020-12-28T00:00Z[Europe/London]
2020-12-28T00:00
2020-12-28T00:00:00.000

Learn about the modern date-time API from Trail: Date Time.

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110