Java — How to get current Date and Time | Code Factory

Get current Date in Java

package com.example.java.programming.datetime;import java.time.LocalDate;/**
* @author code.factory
*
*/
public class CurrentDateExample {
public static void main(String... args) {
LocalDate currentDate = LocalDate.now();
System.out.println("Current Date : " + currentDate);
}
}
Current Date : 2020-06-29

Get current Time in Java

package com.example.java.programming.datetime;import java.time.LocalTime;/**
* @author code.factory
*
*/
public class CurrentTimeExample {
public static void main(String... args) {
LocalTime currentTime = LocalTime.now();
System.out.println("Current Time : " + currentTime);
}
}
Current Time : 14:00:59.165

Get current Date and Time in Java

package com.example.java.programming.datetime;import java.time.LocalDateTime;/**
* @author code.factory
*
*/
public class CurrentDateTimeExample {
public static void main(String... args) {
LocalDateTime currentDateTime = LocalDateTime.now();
System.out.println("Current Date & Time : " + currentDateTime);
}
}
Current Date & Time : 2020-06-29T14:03:56.960

Get current Date and Time in a specific Timezone in Java

package com.example.java.programming.datetime;import java.time.ZoneId;
import java.time.ZonedDateTime;
/**
* @author code.factory
*
*/
public class CurrentDateTimeExample {
public static void main(String... args) {
ZonedDateTime currentKolkataDateTime = ZonedDateTime.now(ZoneId.of("Asia/Kolkata"));
System.out.println("Current Date & Time : " + currentKolkataDateTime);
}
}
Current Date & Time : 2020-06-29T14:11:49.193+05:30[Asia/Kolkata]

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

You Shouldn’t Rely on CSS 100vh and Here’s Why!

Variables: Building blocks of Programming

Things I Wish I Told My Former Founder Self

Biological Engineering: designing life

Do You want to create your own OS (mission 4)

Managing background tasks with new Task Scheduler in iOS 13

Importance of Recon — Hacked one of the biggest Software house within 15mins!

Contract Deployment Automation for Azure API Management integration with PowerApps

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Code Factory

Code Factory

More from Medium

Introduction to Java: A Beginner’s Guide

Resources to learn Java

Java Coding Standards

Are Outsourced Java Developers Still in Demand — Let’s Check Out

Outsourced Java Developers Still in Demand