In Java, How Can I Obtain The Current Date And Time In UTC Or GMT?

To get the current date and time in UTC or GMT, you can use the java.util.Calendar class or the newer java.time package introduced in Java 8.

Using java.util.Calendar:

import java.util.Calendar;
import java.util.TimeZone;

Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
System.out.println(calendar.getTime());  // current date and time in UTC

Using java.time:

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;

LocalDateTime now = LocalDateTime.now();
ZonedDateTime utc = now.atZone(ZoneId.of("UTC"));
System.out.println(utc);  // current date and time in UTC

Both approaches will give you the current date and time in the UTC timezone.

About the Author Rex

I'm a passionate tech blogger with an insatiable love for programming! From my early days tinkering with code, I've delved into web dev, mobile apps, and AI. Sharing insights and tutorials with the world is my joy, connecting me to a global community of like-minded tech enthusiasts. Python holds a special place in my heart, but I embrace all challenges. Constantly learning, I attend tech conferences, contribute to open-source projects, and engage in code review sessions. My ultimate goal is to inspire the next generation of developers and contribute positively to the ever-evolving tech landscape. Let's code together!