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
The date-time API of
java.utiland their formatting API,SimpleDateFormatare outdated and error-prone. Note thatjava.sql.Timestamphas inherited the same drawbacks as it extendsjava.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,
You have also mentioned,
From these two requirements, I infer that you need a date e.g.
2020-12-28combined with the time e.g.00:00:00which is nothing but the start of the day.java.timeprovides a clean API,LocalDate#atStartOfDayto achieve this.Demo:
Output:
Learn about the modern date-time API from Trail: Date Time.