Java basic date manipulation

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

Java basic date manipulation
Date manipulation in Java 8
Java basic grammar
Java basic grammar
Java basic knowledge 1
[Java] Basic structure
[Java] [Basic] Glossary
Java basic grammar
Java basic grammar
Java exercises [Basic]
[Java] Date / time operations
java basic knowledge memo
[Java] Data type ①-Basic type
Java basic naming conventions
Java learning memo (basic)
[Java] Basic method notes
java: Add date [Note]
Java basic data types
Basic Java OOps concepts
[Java] Date type conversion
Java basic learning content 7 (exception)
Java basic syntax + α trap
[Java] Basic statement for beginners
Reflection on Java string manipulation
Java basic learning content 5 (modifier)
Implement Basic authentication in Java
[Java] Date Related Term Memo
Java Basic Learning Content 8 (Java API)
[Beginner] Java basic "array" description
View current date in Java
Java basic learning content 4 (repetition)
[Java] Basic terms in programming
[Java] Basic types and instruction notes
Java release date and EOL summary
Date processing in Java (LocalDate: Initialization)
Java Unit Test Library-Artery-Current Date Judgment
Basic data types and reference types (Java)
Java basic learning content 9 (lambda expression)
Basic usage of java Optional Part 1
Java basic learning content 2 (array / ArrayList)
Java
Reading and writing Java basic files
[MySQL] [java] Receive date and time
[Java] Date period duplication check sample
Basic processing flow of java Stream
Java
Java basic data types and reference types
[Basic knowledge of Java] Scope of variables
[Java] Exception types and basic processing
Basic structure of Java source code
Profiling with Java Visual VM ~ Basic usage ~
Studying Java 8 (date API in java.time package)
Concurrency Method in Java with basic example
[Java] Personal summary of conditional statements (basic)
Basic knowledge of Java development Note writing
[Java] How to set the Date time to 00:00:00
Output Date in Java in ISO 8601 extended format
Java 8 to start now ~ Date time API ~
[Note] Cooperation between Java and DB (basic)
[Java] I personally summarized the basic grammar.
Studying java (Basic Information Technology Engineer Examination)