Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to get system time in Java

Shulou Source: shulou.com Published: 2022-05-31 11:02:45 09月23日 Update

This article focuses on "how to get system time in Java". Friends who are interested may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to get system time in Java.

Examples of specific operations:

1. Get the current time through the Date class.

Date day=new Date ()

SimpleDateFormat df = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss")

System.out.println (df.format (day))

2. Get the current time through the currentTimeMillis method in the System class.

SimpleDateFormat df = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss")

System.out.println (df.format (System.currentTimeMillis ()

3. Get the current time through the Calendar class.

Calendar c = Calendar.getInstance (); / / can be modified separately for each time domain

Int year = c.get (Calendar.YEAR)

Int month = c.get (Calendar.MONTH)

Int date = c.get (Calendar.DATE)

Int hour = c.get (Calendar.HOUR_OF_DAY)

Int minute = c.get (Calendar.MINUTE)

Int second = c.get (Calendar.SECOND)

System.out.println (year + "/" + month + "/" + date + "" + hour + ":" + minute + ":" + second)

4. Get the current date through the Date class.

Date date = new Date ()

String year = String.format ("% tY", date)

String month = String.format ("% tB", date)

String day = String.format ("% te", date)

System.out.println ("Today is:" + year+ "-" + month+ "-" + day)

At this point, I believe you have a deeper understanding of "how to get system time in Java". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Tags: Time system content method learning practical deeper interested practical practical easy to operate date more friends examples websites channels queries Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft Shulou Information Redmi MariaDB Shulou Tech Info