Java获取时间打印到控制台

面试时有一道面试题:要求获取当时时间,并像电子手表那样打印到控制台。

  1 import java.text.SimpleDateFormat;
  2 import java.time.LocalTime;
  3 import java.util.Date;
  4
  5 /**
  6  * 打印时间到控制台
  7  *
  8  */
  9 public class ConsoleTime {
 10
 11     public static void main(String[] args) {
 12
 13 //        LocalTime now = LocalTime.now();
 14 //        System.out.println(now);
 15 //        System.out.println(now.getHour() + " " + now.getMinute() + " " + now.getSecond());
 16
 17         Date date = new Date();
 18         //yyyy-MM-dd
 19         SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");//设置日期格式
 20         String str = format.format(date);
 21         System.out.println("现在时间:" + str);
 22
 23         String[] time = getTime(str.split(":"));
 24         for (int i=0; i<time.length; i++) {
 25             System.out.println(time[i]);
 26         }
 27     }
 28
 29     static String[] getTime(String[] array) {
 30         String[] strArrays = new String[5];
 31         String str1 = "";
 32         String str2 = "";
 33         String str3 = "";
 34         String str4 = "";
 35         String str5 = "";
 36
 37         for (int i=0; i<array.length; i++) {
 38             char[] chars = array[i].toCharArray();
 39             for (int a=0; a<chars.length; a++) {
 40                 switch (chars[a]) {
 41                     case ‘0‘:
 42                         str1 += "   --   ";
 43                         str2 += " |    | ";
 44                         str3 += " |    | ";
 45                         str4 += " |    | ";
 46                         str5 += "   --   ";
 47                         break;
 48                     case ‘1‘:
 49                         str1 += "   |   ";
 50                         str2 += "   |   ";
 51                         str3 += "   |   ";
 52                         str4 += "   |   ";
 53                         str5 += "   |   ";
 54                         break;
 55                     case ‘2‘:
 56                         str1 += "  ----  ";
 57                         str2 += "      | ";
 58                         str3 += "  ----  ";
 59                         str4 += " |      ";
 60                         str5 += "  ----  ";
 61                         break;
 62                     case ‘3‘:
 63                         str1 += "  ----  ";
 64                         str2 += "      | ";
 65                         str3 += "  ----  ";
 66                         str4 += "      | ";
 67                         str5 += "  ----  ";
 68                         break;
 69                     case ‘4‘:
 70                         str1 += " |    | ";
 71                         str2 += " |    | ";
 72                         str3 += "  ----  ";
 73                         str4 += "      | ";
 74                         str5 += "      | ";
 75                         break;
 76                     case ‘5‘:
 77                         str1 += "  ----  ";
 78                         str2 += " |      ";
 79                         str3 += "  ----  ";
 80                         str4 += "      | ";
 81                         str5 += "  ----  ";
 82                         break;
 83                     case ‘6‘:
 84                         str1 += "  ----  ";
 85                         str2 += " |      ";
 86                         str3 += "  ----  ";
 87                         str4 += " |    | ";
 88                         str5 += "  ----  ";
 89                         break;
 90                     case ‘7‘:
 91                         str1 += "  ----   ";
 92                         str2 += "       | ";
 93                         str3 += "       | ";
 94                         str4 += "       | ";
 95                         str5 += "       | ";
 96                         break;
 97                     case ‘8‘:
 98                         str1 += "  ----  ";
 99                         str2 += " |    | ";
100                         str3 += "  ----  ";
101                         str4 += " |    | ";
102                         str5 += "  ----  ";
103                         break;
104                     case ‘9‘:
105                         str1 += "  ----  ";
106                         str2 += " |    | ";
107                         str3 += "  ----  ";
108                         str4 += "      | ";
109                         str5 += "  ----  ";
110                         break;
111                 }
112             }
113             if(i < (array.length-1)) {
114                 str1 += "    ";
115                 str2 += "  . ";
116                 str3 += "    ";
117                 str4 += "  . ";
118                 str5 += "    ";
119             }
120         }
121         strArrays[0] = str1;
122         strArrays[1] = str2;
123         strArrays[2] = str3;
124         strArrays[3] = str4;
125         strArrays[4] = str5;
126         return strArrays;
127     }
128
129 }

打印出来的样式:

原文地址:https://www.cnblogs.com/mxh-java/p/12257570.html

时间: 2024-10-29 00:17:03

Java获取时间打印到控制台的相关文章

Java获取时间 时间计算 转换时间工具类

Java获取时间 时间计算 转换时间工具类 JAVA日期工具类 package com.mh.util; import java.sql.Timestamp; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; /** * 时间日期转换工具类 */ public class DateTimeUtil { /** *

java 获取时间区间

package com.javaCode.package05; import java.text.*; import java.util.*; public class Sd123 { static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); public static void main(String[] args) throws Exception { MyDate m = new MyDa

java获取时间,本周,本月,本季度的起始。

package com.yong.util;import java.util.Calendar;import java.util.Date; public class TestDate { public static void main(String[] args) { // TODO Auto-generated method stub System.out.println("当前时间:"+ new Date().toLocaleString()); System.out.print

java获取时间日期代码

public class test1 { public  static void main(String[] args){ SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd "); //设置日期格式 System.out.println(df.format(new Date())); //输出当前时间 Calendar c = Calendar.getInstance(); //设置日期格式 System.out.printl

java获取时间整点工具代码

/**获取上n个小时整点小时时间 * @param date * @return */ public static String getLastHourTime(Date date,int n){ Calendar ca = Calendar.getInstance(); ca.set(Calendar.MINUTE, 0); ca.set(Calendar.SECOND, 0); SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmm

java获取时间

1.获取年月日 Calendar aa=Calendar.getInstance(); int year=aa.get(Calendar.YEAR); int month=aa.get(Calendar.MONTH+1); 2.整体获取 Date date1=new Date(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Timestamp date=new Timestamp(date1.

java获取时间反转

  public static void main(String[] args) throws Exception {                 SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd");        String str = (sdf.format(new Date()));         char[] dist = new char[str.length()];         for(int x = st

java学习第13天( java获取当前时间,有关大数据的运算及精确数字运算,Date类)

一 java获取当前时间 学习一个函数,得到当前时间的准确值 System.currectTimeMillis(). 可以得到以毫秒为单位的当前时间.它主要用于计算程序运行时间,long start=System.currectTimeMillis() ,long stop=System.currectTimeMillis() , stop-start; 二  有关大数据的运算及精确数字运算. 此时integer不适用.我们使用BigInteger ,如:BigInteger B= new Bi

java 获取前几天时间

java   获取前几天时间 一.toLocaleString()函数过时: //当前时间   Date endDate=new Date();   String endTime=endDate.toLocaleString();      //开始时间   Calendar calendar=new GregorianCalendar();   calendar.setTime(endDate);       calendar.add(calendar.DATE,-day);//把日期往后增加