「java时间程序」时间 java
本篇文章给大家谈谈java时间程序,以及时间 java对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
- 1、编写一个java程序,读取系统时间,然后将时间用中文输出
- 2、Java做一个时间的程序,为什么要除以1000*60*60*24
- 3、用java编写一个控制时间的程序
- 4、JAVA计算时间程序
- 5、java 求一个输出当前时间的程序
- 6、java 如何设定时间执行程序?
编写一个java程序,读取系统时间,然后将时间用中文输出
java程序读取系统时间,可以使用Date时间类,使用格式化类simpleDateFormat类,实例如下:
package com.qiu.lin.he;
import java.text.SimpleDateFormat;
import java.util.Date;
public class CeShi {
public static void main(String[] args) {
Date date = new Date();//获取此时的系统时间
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH点mm分ss秒");
System.out.println("中文现实的时间是" + sdf.format(date));
}
}
运行结果如下:
Java做一个时间的程序,为什么要除以1000*60*60*24
Java一开始生成的时间的单位是毫秒
除以1000单位就变成秒
再除以60单位就变成分钟
再除以60变成小时
再除以24变成天
1442332689043 ÷ 1000 ÷ 60 ÷ 60 ÷ 24 = 1442332689043 ÷ (1000 × 60 × 60 × 24)
如果你想要得到小时,就把24去掉就可以了,根据自己的需求来定。
用java编写一个控制时间的程序
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
public class GetNowTimeDemo{
JFrame frame;
JTextArea displayArea1;
JTextArea displayArea2;
String timestr;
Calendar calendar;
Date currentDate;
SimpleDateFormat sdfyear;//年份格式
SimpleDateFormat sdfmonday;//月份、日期格式
SimpleDateFormat sdfeee;//星期格式
//SimpleDateFormat sdftime;//具体时间格式
String stryear;//年份
String strmonday;//月份、日期
String streee;//星期
String nowTime;
TimeThread timeThread;
SimpleDateFormat sdf=new SimpleDateFormat("hh:mm:ss",Locale.getDefault());
//程序主操作函数
public GetNowTimeDemo(){
frame=new JFrame("现在时间");//显示框容器
frame.setLayout(null);
displayArea1=new JTextArea();//大时间显示框
displayArea1.setFont(new Font("宋体",Font.BOLD,30));//设置显示框字体
displayArea1.setBounds(0,0,400,250);
frame.add(displayArea1);//将时间显示框加载到容器中
displayArea1.setEditable(false);
displayArea2=new JTextArea();//具体时间显示框
displayArea2.setFont(new Font("宋体",Font.BOLD,40));//设置显示框字体
displayArea2.setBounds(0,250,400,100);
frame.add(displayArea2);//将时间显示框加载到容器中
displayArea2.setEditable(false);
displayArea2.setForeground(Color.blue);
JPanel panelCon1=new JPanel();//控制面板1
panelCon1.setBounds(0,350,400,80);
frame.add(panelCon1);//将控制面板加载到容器中
JButton getTime=new JButton("获取时间");//获取时间按钮
panelCon1.add(getTime);//将获取时间按钮加载到控制面板
getTime.addActionListener(new ActionListener(){//获取时间按钮添加动作监听器
public void actionPerformed(ActionEvent e) {//动作监听函数
GetDisTime();//获取并显示时间
}
});
JButton exit=new JButton("退出");//退出按钮
panelCon1.add(exit);//将退出按钮添加到控制面板
exit.addActionListener(new ActionListener(){//退出按钮添加动作监听器
public void actionPerformed(ActionEvent e) {
System.exit(0);//退出
}
});
frame.setLocation(250,50);//设置显示框出现位置
frame.setSize(400,430);//设置显示框大小
frame.setVisible(true);//设置显示框可见性
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置错误处理
}
//获取时间并显示函数
@SuppressWarnings("deprecation")
public void GetDisTime(){
try{
displayArea1.setText("");
calendar=Calendar.getInstance();//获得系统时间日历
currentDate=calendar.getTime();//格式化日历
sdfyear=new SimpleDateFormat("yyyy",Locale.getDefault());//年份格式
stryear=sdfyear.format(currentDate);//年份格式化显示
displayArea1.append("\n\t"+stryear);//年份显示
sdfmonday=new SimpleDateFormat("MMM dd",Locale.getDefault());//月份、日期格式
strmonday=sdfmonday.format(currentDate);//月份、日期格式化显示
displayArea1.append("\n\n\t"+strmonday);//月份、日期显示
sdfeee=new SimpleDateFormat("EEE",Locale.getDefault());//星期格式
streee=sdfeee.format(currentDate);//星期格式化显示
displayArea1.append("\n\n\t"+streee);//星期显示
String time=new Date().toLocaleString();
displayArea1.append("\n"+time);
timeThread=new TimeThread();
timeThread.start();//时间线程启动
//sdftime=new SimpleDateFormat("hh:mm:ss",Locale.getDefault());//具体时间格式
//strtime=sdftime.format(currentDate);//具体时间格式化显示
//displayArea.append("\n\n\t"+strtime);//具体时间显示
//int hour=calendar.get(calendar.HOUR_OF_DAY);
//int minute=calendar.get(calendar.MINUTE);
//int second=calendar.get(calendar.SECOND);
//displayArea.append("\n\n\thour= "+hour+"\n\tminute= "
//+minute+"\n\tsecond= "+second);
}catch(Exception e1){
JOptionPane.showMessageDialog(null,"获取时间出错!");
}
}
class TimeThread extends Thread{
public void run(){
while(true){
try{
calendar=Calendar.getInstance();//获得时间
currentDate=calendar.getTime();//转化为格式化时间
nowTime=sdf.format(currentDate);//将格式化时间转化为设置的String
displayArea2.setText("");
displayArea2.append(" "+nowTime);
Thread.sleep(999);//休眠990us
}catch(Exception e1){
JOptionPane.showMessageDialog(null, "获取时间出错!");
}
}
}
}
//程序主函数
public static void main(String args[]){
new GetNowTimeDemo();
}
}
JAVA计算时间程序
long startTime=System.currentTimeMillis(); //获取开始时间
while(true){
//测试的代码段
long endTime=System.currentTimeMillis(); //获取结束时间
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("程序运行时间: "+(endTime-startTime)/1000+"s");
}
这是计算时间的代码 你可以测试一下看看
java 求一个输出当前时间的程序
System.out.println(new Date());// 设置之前的 虚拟机时间
TimeZone.setDefault(TimeZone.getTimeZone("GMT+8")); //解决虚拟机时间与当前时间的差距
//"GMT+8" 是时区延后8小时
//是否需要增减,得先看下虚拟机时间对不对,正确的话就不需要增减了
//如果虚拟机时间本来就对,就算设置了也是不影响的,可以自己测
Date time = new Date();
System.out.println(time);//设置之后的虚拟机时间
System.out.println(time.getYear()+1900);
System.out.println(time.getMonth()+1);
System.out.println(time.getDate());
System.out.println(time.getHours());
System.out.println(time.getMinutes());
System.out.println(time.getSeconds());
这些方法都是已经被java废弃掉的,现在java推荐的时间操作类是Calendar,这个是必须要跟上时代步伐的,建议去研究研究。
java 如何设定时间执行程序?
import java.util.Calendar;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
public class Test {
public static void main(String[] args) {
//timer1();
timer2();
//timer3();
//timer4();
}
// 第一种方法:设定指定任务task在指定时间time执行 schedule(TimerTask task, Date time)
public static void timer1() {
Timer timer = new Timer();
timer.schedule(new TimerTask() {
public void run() {
System.out.println("-------设定要指定任务--------");
}
}, 2000);// 设定指定的时间time,此处为2000毫秒
}
// 第二种方法:设定指定任务task在指定延迟delay后进行固定延迟peroid的执行
// schedule(TimerTask task, long delay, long period)
public static void timer2() {
Timer timer = new Timer();
timer.schedule(new TimerTask() {
public void run() {
System.out.println("-------设定要指定任务--------");
}
}, 1000, 1000);
}
// 第三种方法:设定指定任务task在指定延迟delay后进行固定频率peroid的执行。
// scheduleAtFixedRate(TimerTask task, long delay, long period)
public static void timer3() {
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
public void run() {
System.out.println("-------设定要指定任务--------");
}
}, 1000, 2000);
}
// 第四种方法:安排指定的任务task在指定的时间firstTime开始进行重复的固定速率period执行.
// Timer.scheduleAtFixedRate(TimerTask task,Date firstTime,long period)
public static void timer4() {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 12); // 控制时
calendar.set(Calendar.MINUTE, 0); // 控制分
calendar.set(Calendar.SECOND, 0); // 控制秒
Date time = calendar.getTime(); // 得出执行任务的时间,此处为今天的12:00:00
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
public void run() {
System.out.println("-------设定要指定任务--------");
}
}, time, 1000 * 60 * 60 * 24);// 这里设定将延时每天固定执行
}
}
java时间程序的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于时间 java、java时间程序的信息别忘了在本站进行查找喔。
发布于:2022-11-26,除非注明,否则均为
原创文章,转载请注明出处。