How Java gets the time to print to the console code
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