導航:首頁 > 編程語言 > java時間24小時制

java時間24小時制

發布時間:2025-07-24 17:45:23

⑴ 用java寫一個程序把24小時制的時間轉換為12小時制的時間.具體說明內詳

importjava.util.Scanner;

{

publicvoidprintException(){
System.out.println("輸入時間錯誤!!程序結束");
}

publicTimeFormatException(){

}

publicvoidprintDate()throwsTimeFormatException{
booleanbStop=true;
Scannerinput=newScanner(System.in);
Stringreg="([0-1][0-9]|2[0-4]):([0-5][0-9])";
while(bStop){
System.out.println("請輸入時間:");
Stringstr=input.next();
if(str.matches(reg)){
inthour=Integer.parseInt(str.substring(0,2));
Stringminutes=str.substring(2,5);
if(hour<12)
System.out.println("現在時間是:"+Integer.toString(hour).concat(minutes)+"am");
elseif(hour==12)
System.out.println("現在時間是:"+Integer.toString(hour).concat(minutes)+"pm");
elseif(hour==24)
System.out.println("現在時間是:"+"00".concat(minutes)+"am");
else
System.out.println("現在時間是:"+Integer.toString(hour-12).concat(minutes)+"pm");
}else{
bStop=false;
thrownewTimeFormatException();
}
}
}

publicstaticvoidmain(String[]args){
try{
newTimeFormatException().printDate();
}catch(TimeFormatExceptione){
e.printException();
}
}

}

如果看不懂 盡管問 [email protected]

⑵ java想要在圖形界面上實現實時顯示時間的功能,可以做成一個類嗎,有沒有示例代碼參考

我之前倒是寫過這么一個,是放在某個類里作為私有類實現的,當然你可以改為一個public的,下面是我的代碼,供參考

	//私有類,實現時間的實時顯示
//利用線程實現實時顯示
{
SimpleDateFormatsdf=newSimpleDateFormat("HH:mm:ss"); //24小時制,精確到秒
publicvoidrun()
{
while(true)
{
time.setText(sdf.format(newDate()));
try
{
Thread.sleep(1000); //線程休眠一秒
}
catch(InterruptedExceptione)
{}
}
}
}

⑶ JAVA里取得當前時間怎麼搞

取得當前年月日
Calendar c=new GregorianCalendar();//新建日期對象
int year=c.get(Calendar.YEAR);//獲取年份
int month=c.get(Calendar.MONTH);//獲取月份
int day=c.get(Calendar.DATE);//獲取日期
int minute=c.get(Calendar.MINUTE);//分
int hour=c.get(Calendar.HOUR);//小時
int second=c.get(Calendar.SECOND);//秒

⑷ 在java中如何將12小時制的時間轉換為24小時制

Java中將12小時制的時間轉換為24小時制的方式如下:

importjava.text.SimpleDateFormat;
importjava.util.Date;

publicclassceshi{
publicstaticvoidmain(String[]args){
=newSimpleDateFormat(
"yyyy-MM-ddHH:mm:ss");//轉換為24小時制
StringstrCurrentTime=objSDateFormat.format(newDate());
System.out.println(strCurrentTime);
}

}

註:大寫的HH為24小時制,小寫的hh為12小時制,當然還可以在ss的後面加上 a,這樣可以在後面顯示上下文:顯示效果為「2008-03-24 17:00:14 下午」

運行結果為:

⑸ java 中cal.get(Calendar.HOUR_OF_DAY) 取小時 如果是中午12點取值為0 我怎麼樣才能取到12

cal.get(Calendar.HOUR_OF_DAY) 取的就是24時鍾數
cal.get(Calendar.HOUR) 取的就是12時鍾數

看一下它們的注釋就清楚了。
/**
* Field number for <code>get</code> and <code>set</code> indicating the
* hour of the morning or afternoon. <code>HOUR</code> is used for the
* 12-hour clock (0 - 11). Noon and midnight are represented by 0, not by 12.
* E.g., at 10:04:15.250 PM the <code>HOUR</code> is 10.
*
* @see #AM_PM
* @see #HOUR_OF_DAY
*/
public final static int HOUR = 10;

/**
* Field number for <code>get</code> and <code>set</code> indicating the
* hour of the day. <code>HOUR_OF_DAY</code> is used for the 24-hour clock.
* E.g., at 10:04:15.250 PM the <code>HOUR_OF_DAY</code> is 22.
*
* @see #HOUR
*/
public final static int HOUR_OF_DAY = 11;

如果使用的是cal.get(Calendar.HOUR),可能通過cal.get(Calendar.AM_PM)的返回值來判斷是上午還是下午,如果返回0,則是上午,如果返回1,則是下午。判斷的時候,最好用類定義的常量來比較。
if(Calendar.AM==cal.get(Calendar.AM_PM)){
//上午
}else /*if(Calendar.PM==cal.get(Calendar.AM_PM))*/{
//下午
}

⑹ java怎麼實現輸出一天當中的0-24小時

// 從鍵盤輸入24小時制時間轉換為12小時制並輸出 我這里是用死的 時間, 你可以用控制台輸入的方式來模擬用戶輸入
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
Map<String, String> hMap = new HashMap<String, String>();
hMap.put("13", "1");
hMap.put("14", "2");
hMap.put("15", "3");
hMap.put("16", "4");
hMap.put("17", "5");
hMap.put("18", "6");
hMap.put("19", "7");
hMap.put("20", "8");
hMap.put("21", "9");
hMap.put("22", "10");
hMap.put("23", "11");
hMap.put("24", "00");

String time = "23:30:23";
String[] tList = time.split(":");
String h = hMap.get(tList[0]);
h = h == null ? tList[0] : h;

String newTime = h + ":" + tList[1] + ":" + tList[2];
System.out.println(newTime);

閱讀全文

與java時間24小時制相關的資料

熱點內容
vss資料庫是什麼 瀏覽:899
奇跡13單機系統找不到指定文件 瀏覽:719
flyme魅藍3以前的版本 瀏覽:318
安卓文件管理哪些文件夾可以刪除 瀏覽:290
安卓車載導航沒有聲音是怎麼回事 瀏覽:810
cjson數組格式 瀏覽:159
vb文件在哪裡 瀏覽:215
工廠里都招什麼編程人員 瀏覽:932
jspsql登錄 瀏覽:981
網路用語粉絲閱讀什麼意思 瀏覽:333
紅頭文件怎麼列印 瀏覽:94
熱血江湖130刺客升級 瀏覽:106
jsp頁面放大鏡技術介紹 瀏覽:101
網路編程udp 瀏覽:148
加密壓縮文件如何打開 瀏覽:56
微軟編程軟體有哪些 瀏覽:736
linux目錄中創建文件夾許可權設置密碼 瀏覽:759
word文檔正式文件模版 瀏覽:247
linux文件系統的類型是 瀏覽:111
蘋果的無線傳輸文件找不到了 瀏覽:102

友情鏈接