Wednesday 21 February 2018

How to format date and time using format method of ZonedDateTime Class | Java 8 Date and Time


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

ZoneDateTimeDemo.java

import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;

public class ZoneDateTimeDemo
{

    public static void main(String[] args)
    {

        ZonedDateTime zonedDateTime = ZonedDateTime.now();
        System.out.println(zonedDateTime);
       
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ISO_DATE;

        /*
         * Parameters:
         *
         * formatter - the formatter to use, not null
         *
         * Returns:
         *
      &nbs p;  * the formatted date-time string, not null
         */

        String formatedDateTime = zonedDateTime.format(dateTimeFormatter);
        System.out.println("ISO_DATE = "+formatedDateTime);
       
        dateTimeFormatter = DateTimeFormatter.ISO_TIME;
        formatedDateTime = zonedDateTime.format(dateTimeFormatter);
        System.out.println("ISO_TIME = "+formatedDateTime);
    }

}

Output

2018-02-07T10:13:52.561+05:30[Asia/Calcutta]
ISO_DATE = 2018-02-07+05:30
ISO_TIME = 10:13:52.561+05:30

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

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

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/914905cc2357de2bddb749208190c8f0bf0f9978/BasicJava/ZoneDateTimeDemo_format/?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