您好,登錄后才能下訂單哦!
這篇文章主要介紹了Java如何獲取時(shí)間打印到控制臺代碼,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
面試時(shí)有一道面試題:要求獲取當(dāng)時(shí)時(shí)間,并像電子手表那樣打印到控制臺。
import java.text.SimpleDateFormat;import java.time.LocalTime;import java.util.Date;/** * 打印時(shí)間到控制臺 * */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");//設(shè)置日期格式 String str = format.format(date); System.out.println("現(xiàn)在時(shí)間:" + 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; }}
感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“Java如何獲取時(shí)間打印到控制臺代碼”這篇文章對大家有幫助,同時(shí)也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,更多相關(guān)知識等著你來學(xué)習(xí)!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。