⑴ java事件触发计时器后二十四小时后只执行一次业务逻辑
java中有个定时任务 java.util.TimerTask
用法很简单
classMyTaskextendsjava.util.TimerTask{
@Override
publicvoidrun(){
//这个任何所执行的版代码
}
}
java.util.Timertimer=newTimer(true); //treu就是守护线程
MyTasktask=newMyTask();
//开始执行任务权,第一个参数是任务,第二个是延迟时间,第三个是每隔多长时间执行一次
timer.schele(task,0,1000*60*60*24);
⑵ 用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 获取当前时间的小时(24小时制)
使用new Date()获取时间,通过SimpleDateFormat格式化类对Date进行格式话时间。
具体代码如下:注意HH大写代表24小时制。
输出结果:1510416000000,2017-11-12。方便的实现了string转时间的功能。
⑷ 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将12小时制改为24小时制,Java新手
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:dd:mm");
System.out.println(sdf.format(new Date()));
这个是将当前时间的格式改为yyyy-MM-dd HH:dd:mm的,显示的是24小时制。
⑹ 在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 下午”
运行结果为:
⑺ SimpleDateFormat 12小时制和24小时制的区别
格式化时间为12小时制的,则使用hh:mm:ss如果希望格式化时间为24小时制的,则使用HH:mm:ss
在使用SimpleDateFormat时格式化时间的yyyy.MM.dd 为年月日而如果希望格式化时间为12小时制的,则使用hh:mm:ss如果希望格式化时间为24小时制的,则使用HH:mm:ss
Datedate = new Date();
SimpleDateFormat sdformat = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss a ZZZ");
String LgTime = formatter.format(LoginDate1);
结过为24小时:星期四 2005.07.14 11:07:812 上午 +0800
Date类,已经很少用了。更多使用的是Calendar
Calendar date = Calendar.getInstance();
date.get(Calendar.HOUR_OF_DAY );//得到24小时机制的
date.get(Calendar.HOUR);// 得到12小时机制的
当然,SimpleDateFormat也可以格式化24机制或者12小时机制。
H 0-23
k 1-24
----------------
K 0-11
h 1-12
Java是一门面向对象编程语言,不仅吸收了C++语言的各种优点,还摒弃了C++里难以理解的多继承、指针等概念,因此Java语言具有功能强大和简单易用两个特征。Java语言作为静态面向对象编程语言的代表,极好地实现了面向对象理论,允许程序员以优雅的思维方式进行复杂的编程 。
Java具有简单性、面向对象、分布式、健壮性、安全性、平台独立与可移植性、多线程、动态性等特点 。Java可以编写桌面应用程序、Web应用程序、分布式系统和嵌入式系统应用程序等 。
⑻ JAVA:怎样把24小时制转换成12小时制
SimpleDateFormat objSDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String strCurrentTime = objSDateFormat.format(Date类型的时告羡间);
注带亩:大写的HH为24小时制,小写的hh为12小时制,当然还可以在ss的后面加上 a,这样可以袜行拍在后面显示上下文:显示效果为“2008-03-24 17:00:14 下午”
⑼ 在Java中,以下哪种定义,可以得到4位年2位月2位日,24小时制的格式()(2分)
LocalDateTime dateTime = LocalDateTime.now();
String str = dateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
System.out.println(str);