Programs dealing with people, time, goods and money become difficult
I dealt with time in such a situation, so a memo
First, to use dates in Java, you need to import:
import java.util.Date;
After that, the usual instantiation
//Date instantiation
Date date = new Date();
So, this time, I will set up a process that makes the past day an error, so I added two processes. This time, it seems that the error is checked only by the date and only the time, so I also imported the following files.
import java.text.ParseException;
import java.text.SimpleDateFormat;
java.text.ParseException is a guy who changes from date type to string. java.text.SimpleDateFormat is a guy who can change it to something like "YYYY-MM-DD".
String nowDate = sdFormat.format(date);
First, get the current date with SimpleDateFormat. It's convenient to be able to get the date from the server now.
checkDate = sdFormat.parse(checkStr);
Then, ParseException converts String type to Data type. This will get angry from eclipce if you don't try / catch.
That's why I tried some trial and error here.
if (date.after(checkDate)) {
if(!nowDate.equals(checkStr)) {
//Write the processing at the time of error
}
}
If the date is the same as date.after, an error will occur. This will result in an error if "Selected date> Date".
I could build PHP without worrying about type operations, but Java is hard because I have to declare the type tightly and specify it when changing it.
And HTML5 written in JSP ...
<input type="date" name="date" required value=<%=dateStr %>>
The date type is very convenient.
If you enter a value in value, it will be set as the initial value.
In the past, I would have added a comment such as "Please enter an 8-digit number." It is very easy because it skips the value in "YYYY-MM-DD" format.
The calendar is displayed and can be selected on the operating side, and it was very convenient because both sides had an advantage.
Recommended Posts