Last active
September 22, 2017 15:06
-
-
Save javamultiplex/8775886bd0c799783524a33c2ea0964f to your computer and use it in GitHub Desktop.
How to print current date and time in Java?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.javamultiplex.datetime; | |
import java.time.LocalDate; | |
import java.time.LocalDateTime; | |
import java.time.ZonedDateTime; | |
import java.util.Calendar; | |
import java.util.Date; | |
/** | |
* | |
* @author Rohit Agarwal | |
* @category Date and Time | |
* @problem How to print current date and time? | |
* | |
*/ | |
public class CurrentDateAndTime { | |
public static void main(String[] args) { | |
// Creating Date class instance | |
Date date = new Date(); | |
System.out.println("Current Date and Time by Date class : " + date); | |
// Creating Calendar class instance | |
Calendar calendar = Calendar.getInstance(); | |
System.out.println("Current Date and Time by Calendar class : " + calendar.getTime()); | |
/* | |
* Using Java 8 API classes. | |
* 1) LocalDate -> Print date | |
* 2) LocalDateTime-> Print date and Time | |
* 3) ZonedDateTime -> Print date, time and timezone | |
*/ | |
LocalDate localDate = LocalDate.now(); | |
System.out.println("Current Date by LocalDate class : " + localDate); | |
LocalDateTime localDateTime = LocalDateTime.now(); | |
System.out.println("Current Date and Time by LocalDateTime class : " + localDateTime); | |
ZonedDateTime zonedDateTime = ZonedDateTime.now(); | |
System.out.println("Current Date, Time and Timezone by ZonedDateTime class : " + zonedDateTime); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment