I tried to convert a string to a LocalDate type in Java

This is an example of how to convert a String type date to a LocalDate type in Java. The background of this investigation is that while implementing an API using Java Persistence API (JPA), This is because it was necessary to convert the character string of yyyyMMdd format stored in String type to LocalDate type.

Premise

This post uses Java version 8. The content to be introduced is just an example, so I don't know.

Character string "yyyyMMdd" → Conversion to LocalDate type

For the time being, create the following method

Sample.java


//Format string date(yyyyMMdd and yyyy/mm/dd etc.)Method to convert to LocalDate type based on
public static LocalDate convertToLocalDate(String date,String format) {

                //Simply return the date converted to LocalDate type
		return LocalDate.parse(date, DateTimeFormatter.ofPattern(format));

	}

Output to check the result

Sample.java


public class Sample {
	
	public static void main (String args[]) {
	
		System.out.println(convertToLocalDate("19990707", "yyyyMMdd"));

	}

The output result looks like this

result


1999-07-07

The class DateTimeFormatter seems to have a predefined formatter, Here is an example using that

Sample2.java


//Change arguments to an instance of the string date and DateTimeFormatter classes
public static LocalDate convertStringToLocalDate(String date,DateTimeFormatter formatter) {

		return LocalDate.parse(date,formatter);

	}

This is the formatter used this time → BASIC_ISO_DATE (The description of "basic ISO date" was on the oracle page) In short, BASIC_ISO_DATE seems to be able to carry a formatter in the form of "yyyymmdd".

Sample2.java


public class Sample2 {
	
	public static void main (String args[]) {
	
		System.out.println(convertStringToLocalDate("18980401", DateTimeFormatter.BASIC_ISO_DATE));

	}

The result will be the same as Sample.java

result


1898-04-01
Other

Since this is my first post, I'm new to posting, but I'd like to continue patiently. Advice and suggestions are welcome

Recommended Posts

I tried to convert a string to a LocalDate type in Java
I tried to create a Clova skill in Java
Convert a Java byte array to a string in hexadecimal notation
[Java] How to convert a character string from String type to byte type
I tried to make a client of RESAS-API in Java
[Java] Convert Object type null to String type
I tried to write code like a type declaration in Ruby
java I tried to break a simple block
Code to escape a JSON string in Java
I tried to implement deep learning in Java
I wanted to make (a == 1 && a == 2 && a == 3) true in Java
I tried to output multiplication table in Java
I tried to create Alexa skill in Java
I tried to break a block with java (1)
I tried to make a talk application in Java using AI "A3RT"
Convert String type to Timestamp type
I tried metaprogramming in Java
Sample code to convert List to List <String> in Java Stream
I tried to implement Firebase push notification in Java
# 2 [Note] I tried to calculate multiplication tables in Java.
How to convert A to a and a to A using AND and OR in Java
How to convert a file to a byte array in Java
I tried to implement the Euclidean algorithm in Java
~ I tried to learn functional programming in Java now ~
I tried to find out what changed in Java 9
[Java] How to convert one element of a String type array to an Int type
I tried to create a java8 development environment with Chocolatey
I tried to modernize a Java EE application with OpenShift.
[Java] I want to convert a byte array to a hexadecimal number
[Beginner] I made a program to sell cakes in Java
I want to convert InputStream to String
I just wanted to make a Reactive Property in Java
I tried to interact with Java
How to store a string from ArrayList to String in Java (Personal)
Even in Java, I want to output true with a == 1 && a == 2 && a == 3
I tried using JWT in Java
I tried to summarize Java learning (1)
I tried using Dapr in Java to facilitate microservice development
I tried to implement a buggy web application in Kotlin
I tried to summarize Java 8 now
I tried using Java memo LocalDate
I wrote a code to convert numbers to romaji in TDD
A memorandum when I investigated how to convert from Epoch Time to Date type in Scala
I tried to create a simple map app in Android Studio
I tried to illuminate the Christmas tree in a life game
[Ruby] I want to put an array in a variable. I want to convert to an array
I tried setting Java beginners to use shortcut keys in eclipse
[Convenient to remember !!!] How to convert from LocalDate type to character string and from character string to LocalDate type
I tried to summarize the methods of Java String and StringBuilder
[Java] I tried to make a maze by the digging method ♪
[Java] How to convert from String to Path type and get the path
I tried using Elasticsearch API in Java
I tried a calendar problem in Ruby
Regarding String type equivalence comparison in Java
I tried to summarize Java lambda expressions
I tried the new era in Java
Convert SVG files to PNG files in Java
Notation to put a variable in a string
Split a string with ". (Dot)" in Java
Type conversion from java BigDecimal type to String type
I tried embedding a formula in Javadoc