Java date data type conversion (Date, Calendar, String)

When dealing with date data in Java, I was often confused, so I tried to organize it myself.

Data type for date

Date

Calendar

java.util.Date

java.sql.Date

Date ⇄ string conversion

Date→String

Date date = new Date(); //Today's date
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
String strDate = dateFormat.format(date);

System.out.println(date); // => Mon Dec 31 10:33:59 JST 2018
System.out.println(strDate); // => 2018-12-31
Date date = new Date(); //Today's date
String strDate = date.toString();

System.out.println(date); // => Mon Dec 31 10:33:59 JST 2018
System.out.println(strDate); // => Mon Dec 31 10:33:59 JST 2018

String→Date

public static void main(String[] args) throws ParseException{
   String strDate = "2018-12-31";
   SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
   Date date = dateFormat.parse(strDate);

   System.out.println(date); // => Mon Dec 31 00:00:00 JST 2018
}

String strDate = "2018-12-31";
Date sqlDate = java.sql.Date.valueOf(strDate);

System.out.println(sqlDate); // => 2018-12-31

Date⇄Calendar conversion

Date→Calendar

Date date = new Date(); //Today's date
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);

System.out.println(calendar);
// =>java.util.GregorianCalendar[time=1546228443561,areFieldsSet=true, ...]

Calendar→Date

Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR, 2018); //Set year
calendar.set(Calendar.MONTH, 11); //Set month(* MONTH starts from 0 0 → Jan, 1 → Feb,...become)
calendar.set(Calendar.DAY_OF_MONTH, 31);//Set date
Date date = calendar.getTime();

System.out.println(date); // => Mon Dec 31 13:01:05 JST 2018

Reference site

at the end

I summarized how to handle date data that I have implemented. I would be grateful if you could give me some advice if you say "this is better" or "I often do this"! Wish you a good year!

Recommended Posts

Java date data type conversion (Date, Calendar, String)
Java type conversion (String, int, Date, Calendar, etc.)
[Java] Date type conversion
Java type conversion
[Java] Data type / string class cheat sheet
Java 8 LocalDateTime type conversion stuff (String, java.util.Date)
Type conversion from java BigDecimal type to String type
[Java] Data type ①-Basic type
Uri → String, String → Uri type conversion
[Java] List type / Array type conversion
Java learning memo (data type)
Conversion of String, Date, LocalDate
[Java] Precautions for type conversion
[Java] Type conversion speed comparison
Java Primer Series (Type Conversion)
[Java] Data type / matrix product (AOJ ⑧ Matrix product)
[Java] Correct comparison of String type
Java string
Full-width → half-width conversion with Java String (full-width kana → half-width kana)
Regarding String type equivalence comparison in Java
[Easy-to-understand explanation! ] Reference type type conversion in Java
Use PostgreSQL data type (jsonb) from Java
[Java ~ Variable definition, type conversion ~] Study memo
[Personal memo] Java data type is annoying
Java study # 3 (type conversion and instruction execution)
Notes on operators using Java ~ String type ~
[Java] Convert Object type null to String type
[Basic knowledge of Java] About type conversion
[Java] Comparison of String type character strings
[Java] Calculation mechanism, operators and type conversion
Flexible data type conversion mechanism of O / R mapping library Lightsleep for Java 8
[Introduction to Java] About type conversion (cast, promotion)
[JAVA] Stream type
[Java] String padding
[Java] Enumeration type
Java Optional type
[Processing x Java] Data type and object-oriented programming
Java double type
Java string processing
Date () and Calendar ()
Organized memo in the head (Java --Data type)
Java-automatic type conversion
Split string (Java)
[Java] How to use Calendar class and Date class
Java variable declaration, initialization, data type (cast and promotion)
[Java] Get the date 10 days later with the Calendar class
About Java basic data types and reference type memory
[Java] Full-width ⇔ half-width conversion
[Java] Date / time operations
URL to String conversion
[Java] String comparison and && and ||
Java string multiple replacement
Java basic date manipulation
[Java, Kotlin] Type Variance
Java class type field
[Note] Java: String search
Type determination in Java
Java study # 1 (typical type)
[Note] Java: String survey
About Java String class
[Java] Main data types