Get the App
SLTechnology News&Howtos  ›  Development  › 

How Java gets the time to print to the console code

Shulou Source: shulou.com Published: 2022-06-03 09:40:53 10月02日 Update

This article mainly introduces Java how to get time to print to the console code, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let Xiaobian take you to understand.

There is an interview question during the interview: ask to get the time and print it to the console like an electronic watch.

Import java.text.SimpleDateFormat;import java.time.LocalTime;import java.util.Date;/** * print time to console * * / public class ConsoleTime {public static void main (String [] args) {/ / LocalTime now = LocalTime.now (); / / System.out.println (now); / / System.out.println (now.getHour () + "+ now.getMinute () +" + now.getSecond ()); Date date = new Date () / / yyyy-MM-dd SimpleDateFormat format = new SimpleDateFormat ("HH:mm:ss"); / / set the date format String str = format.format (date); System.out.println ("now time:" + str); String [] time = getTime (str.split (":")); for (int item0; I

Tags: Console time control article code one value interest at the same time watch date more friends format present tense electronics electronic watch knowledge ribbon industry Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi Linux OPPO Reno Xiaomi vpn