您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
这篇文章主要介绍了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<time.length; 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<array.length; i++) { char[] chars = array[i].toCharArray(); for (int a=0; a<chars.length; 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 += " ---- "; break; case '6': str1 += " ---- "; str2 += " | "; str3 += " ---- "; str4 += " | | "; str5 += " ---- "; break; case '7': str1 += " ---- "; str2 += " | "; str3 += " | "; str4 += " | "; str5 += " | "; 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; }}
感谢你能够认真阅读完这篇文章,希望小编分享的“Java如何获取时间打印到控制台代码”这篇文章对大家有帮助,同时也希望大家多多支持亿速云,关注亿速云行业资讯频道,更多相关知识等着你来学习!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。