Java获取时间打印到控制台代码实例

网友投稿 350 2022-12-15


Java获取时间打印到控制台代码实例

这篇文章主要介绍了java获取时间打印到控制台代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

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

import java.text.SimpleDateFormat;

import java.time.LocalTime;

import java.util.Date;

/**

* 打印时间到控制台

*

*/

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");//设置日期格式

String str = format.format(date);

System.out.println("现在时间:" + str);

String[] time = getTime(str.split(":"));

for (int i=0; i

System.out.println(time[i]);

}

}

static String[] getTime(String[] array) {

String[] strArrays = new String[5];

String str1 = "";

String str2 = "";

String str3 = "";

String str4 = "";

String str5 = "";

for (int i=0; i

char[] chars = array[i].toCharArray();

for (int a=0; a

switch (chars[a]) {

case '0':

str1 += " -- ";

str2 += " | | ";

str3 += " | | ";

str4 += " | | ";

str5 += " -- ";

break;

case '1':

str1 += " | ";

str2 += " | ";

str3 += " | ";

str4 += " | ";

str5 += " | ";

break;

case '2':

str1 += " ---- ";

str2 += " | ";

str3 += " ---- ";

str4 += " | ";

str5 += " ---- ";

break;

case '3':

str1 += " ---- ";

str2 += " | ";

str3 += " ---- ";

str4 += " | ";

str5 += " ---- ";

break;

case '4':

str1 += " | | ";

str2 += " | | ";

str3 += " ---- ";

str4 += " | ";

str5 += " | ";

break;

case '5':

str1 += " ---- ";

str2 += " | ";

str3 += " ---- ";

str4 += " | ";

str5 += " ---- ";

http:// break;

case '6':

str1 += " ---- ";

str2 += " | ";

str3 += " ---- ";EOJXKua

str4 += " | | ";

str5 += " ---- ";

break;

case '7':

str1 += " ---- ";

str2 += " | ";

str3 += " | ";

str4 += " | ";

str5 += " | ";

EOJXKua break;

case '8':

str1 += " ---- ";

str2 += " | | ";

str3 += " ---- ";

str4 += " | | ";

str5 += " ---- ";

break;

case '9':

str1 += " ---- ";

str2 += " | | ";

str3 += " ---- ";

str4 += " | ";

str5 += " ---- ";

break;

}

}

if(i < (array.length-1)) {

str1 += " ";

str2 += " . ";

str3 += " ";

str4 += " . ";

str5 += " ";

}

}

strArrays[0] = str1;

strArrays[1] = str2;

strArrays[2] = str3;

strArrays[3] = str4;

strArrays[4] = str5;

return strArrays;

}

}

打印出来的样式:

http://

System.out.println(time[i]);

}

}

static String[] getTime(String[] array) {

String[] strArrays = new String[5];

String str1 = "";

String str2 = "";

String str3 = "";

String str4 = "";

String str5 = "";

for (int i=0; i

char[] chars = array[i].toCharArray();

for (int a=0; a

switch (chars[a]) {

case '0':

str1 += " -- ";

str2 += " | | ";

str3 += " | | ";

str4 += " | | ";

str5 += " -- ";

break;

case '1':

str1 += " | ";

str2 += " | ";

str3 += " | ";

str4 += " | ";

str5 += " | ";

break;

case '2':

str1 += " ---- ";

str2 += " | ";

str3 += " ---- ";

str4 += " | ";

str5 += " ---- ";

break;

case '3':

str1 += " ---- ";

str2 += " | ";

str3 += " ---- ";

str4 += " | ";

str5 += " ---- ";

break;

case '4':

str1 += " | | ";

str2 += " | | ";

str3 += " ---- ";

str4 += " | ";

str5 += " | ";

break;

case '5':

str1 += " ---- ";

str2 += " | ";

str3 += " ---- ";

str4 += " | ";

str5 += " ---- ";

http:// break;

case '6':

str1 += " ---- ";

str2 += " | ";

str3 += " ---- ";EOJXKua

str4 += " | | ";

str5 += " ---- ";

break;

case '7':

str1 += " ---- ";

str2 += " | ";

str3 += " | ";

str4 += " | ";

str5 += " | ";

EOJXKua break;

case '8':

str1 += " ---- ";

str2 += " | | ";

str3 += " ---- ";

str4 += " | | ";

str5 += " ---- ";

break;

case '9':

str1 += " ---- ";

str2 += " | | ";

str3 += " ---- ";

str4 += " | ";

str5 += " ---- ";

break;

}

}

if(i < (array.length-1)) {

str1 += " ";

str2 += " . ";

str3 += " ";

str4 += " . ";

str5 += " ";

}

}

strArrays[0] = str1;

strArrays[1] = str2;

strArrays[2] = str3;

strArrays[3] = str4;

strArrays[4] = str5;

return strArrays;

}

}

打印出来的样式:

http://

char[] chars = array[i].toCharArray();

for (int a=0; a

switch (chars[a]) {

case '0':

str1 += " -- ";

str2 += " | | ";

str3 += " | | ";

str4 += " | | ";

str5 += " -- ";

break;

case '1':

str1 += " | ";

str2 += " | ";

str3 += " | ";

str4 += " | ";

str5 += " | ";

break;

case '2':

str1 += " ---- ";

str2 += " | ";

str3 += " ---- ";

str4 += " | ";

str5 += " ---- ";

break;

case '3':

str1 += " ---- ";

str2 += " | ";

str3 += " ---- ";

str4 += " | ";

str5 += " ---- ";

break;

case '4':

str1 += " | | ";

str2 += " | | ";

str3 += " ---- ";

str4 += " | ";

str5 += " | ";

break;

case '5':

str1 += " ---- ";

str2 += " | ";

str3 += " ---- ";

str4 += " | ";

str5 += " ---- ";

http:// break;

case '6':

str1 += " ---- ";

str2 += " | ";

str3 += " ---- ";EOJXKua

str4 += " | | ";

str5 += " ---- ";

break;

case '7':

str1 += " ---- ";

str2 += " | ";

str3 += " | ";

str4 += " | ";

str5 += " | ";

EOJXKua break;

case '8':

str1 += " ---- ";

str2 += " | | ";

str3 += " ---- ";

str4 += " | | ";

str5 += " ---- ";

break;

case '9':

str1 += " ---- ";

str2 += " | | ";

str3 += " ---- ";

str4 += " | ";

str5 += " ---- ";

break;

}

}

if(i < (array.length-1)) {

str1 += " ";

str2 += " . ";

str3 += " ";

str4 += " . ";

str5 += " ";

}

}

strArrays[0] = str1;

strArrays[1] = str2;

strArrays[2] = str3;

strArrays[3] = str4;

strArrays[4] = str5;

return strArrays;

}

}

打印出来的样式:

http://

switch (chars[a]) {

case '0':

str1 += " -- ";

str2 += " | | ";

str3 += " | | ";

str4 += " | | ";

str5 += " -- ";

break;

case '1':

str1 += " | ";

str2 += " | ";

str3 += " | ";

str4 += " | ";

str5 += " | ";

break;

case '2':

str1 += " ---- ";

str2 += " | ";

str3 += " ---- ";

str4 += " | ";

str5 += " ---- ";

break;

case '3':

str1 += " ---- ";

str2 += " | ";

str3 += " ---- ";

str4 += " | ";

str5 += " ---- ";

break;

case '4':

str1 += " | | ";

str2 += " | | ";

str3 += " ---- ";

str4 += " | ";

str5 += " | ";

break;

case '5':

str1 += " ---- ";

str2 += " | ";

str3 += " ---- ";

str4 += " | ";

str5 += " ---- ";

http:// break;

case '6':

str1 += " ---- ";

str2 += " | ";

str3 += " ---- ";EOJXKua

str4 += " | | ";

str5 += " ---- ";

break;

case '7':

str1 += " ---- ";

str2 += " | ";

str3 += " | ";

str4 += " | ";

str5 += " | ";

EOJXKua break;

case '8':

str1 += " ---- ";

str2 += " | | ";

str3 += " ---- ";

str4 += " | | ";

str5 += " ---- ";

break;

case '9':

str1 += " ---- ";

str2 += " | | ";

str3 += " ---- ";

str4 += " | ";

str5 += " ---- ";

break;

}

}

if(i < (array.length-1)) {

str1 += " ";

str2 += " . ";

str3 += " ";

str4 += " . ";

str5 += " ";

}

}

strArrays[0] = str1;

strArrays[1] = str2;

strArrays[2] = str3;

strArrays[3] = str4;

strArrays[4] = str5;

return strArrays;

}

}

打印出来的样式:

http://


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Java 腾讯验证码平台使用实例
下一篇:Java重写(Override)与重载(Overload)区别原理解析
相关文章

 发表评论

暂时没有评论,来抢沙发吧~