Wednesday 24 January 2018

How to convert string date to LocalDateTime using parse method of LocalDateTime Class


Click here to watch on Youtube : 
https://www.youtube.com/watch?v=RrZ-mTwCCH0&list=UUhwKlOVR041tngjerWxVccw

LocalDateTimeDemo1.java

import java.time.LocalDateTime;

public class LocalDateTimeDemo1
{

    public static void main(String[] args)
    {

        String strDate = "2020-02-03T10:15:30";
        /*
         * Parameters:
         *
         * text - the text to parse such as "2007-12-03T10:15:30", not
         * null
         *
         * Returns:the parsed local date-time, not null
         */

        LocalDateTime localDateTime = LocalDateTime.parse(strDate);

        System.out.println(localDateTime);

    }

}

Output

2020-02-03T10:15:30

LocalDateTimeDemo2.java

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class LocalDateTimeDemo2
{

    public static void main(String[] args)
    {

        String strDate = "2017-12-03T10:15:30+01:00";
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ISO_DATE_TIME;

        /*
         * Parameters:
         *
         * text - the text to parse, not null
         *
         * formatter - the formatter to use, not null
         *
         * Returns:
         *
         * the parsed local date-time, not null
         */


        LocalDateTime localDateTime = LocalDateTime.parse(strDate,
                dateTimeFormatter);

        System.out.println(localDateTime);
    }

}

Output

2017-12-03T10:15:30

Click the below link to download the code:
https://sites.google.com/site/ramj2eev1/home/javabasics/LocalDateTimeDemo_parse.zip?attredirects=0&d=1

Github Link:
https://github.com/ramram43210/Java/tree/master/BasicJava/LocalDateTimeDemo_parse

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/2732bd5046563d647669928b143687e5dbf4d99f/BasicJava/LocalDateTimeDemo_parse/?at=master

See also:
  • All JavaEE Videos Playlist
  • All JavaEE Videos
  • All JAVA EE Links
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • Kids Tutorial
  • No comments:

    Post a Comment