⑴ 用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);