Java 如何显示当前系统日期与时间

Java 如何显示当前系统日期与时间,第1张

通过new Date获取当前的日期时间

示例:

public static void main(String[] args){ 

Date now = new Date(); //获取当前时间

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");//格式化当前日期时间,显示如2015/06/27 14:22:22

}

获取internet标准时间,参考以下代码:

TimeZonesetDefault(TimeZonegetTimeZone("GMT+8")); // 时区设置

URL url=new URL("

);//取得资源对象

URLConnection uc=urlopenConnection();//生成连接对象

ucconnect(); //发出连接

long ld=ucgetDate(); //取得网站日期时间(时间戳)

Date date=new Date(ld); //转换为标准时间对象

//分别取得时间中的小时,分钟和秒,并输出

Systemoutprint(dategetHours()+"时"+dategetMinutes()+"分"+dategetSeconds()+"秒");

import javautilDate;

import orgapachecommonslang3timeDateUtils;

public class Test {

public static void main(String[] args) {

Date now = new Date();

// 减一小时

Date add = DateUtilsaddHours(now, -1);

// 加一小时

Date sub = DateUtilsaddHours(now, 1);

if (addafter(now)) {

Systemoutprintln("当前时间后");

} else {

Systemoutprintln("当前时间前");

}

if (subbefore(now)) {

Systemoutprintln("当前时间前");

} else {

Systemoutprintln("当前时间后");

}

}

}

public static void main(String[] args)

{

ActionListener time = new ActionListener() { // 监听事件,不然实现不了动态改变时间

public void actionPerformed(ActionEvent e) {

//date对象代表当前的系统时间(毫秒)

Date date = new Date();

//format对象是用来以指定的时间格式格式化时间的

SimpleDateFormat from = new SimpleDateFormat(

"yyyy-MM-dd HH:mm:ss"); //这里的格式可以自己设置

//format()方法是用来格式化时间的方法

String times = fromformat(date);

Systemoutprintln(times); }

};

Timer tim = new Timer(1000, time); //这里表示1000毫秒更新一下时间

timstart(); //启动

}

我这个答案肯定正确啊

下面帮你解释你的答案吧

Date //是在javautilDate;里面

SimpleDateForma //这个是javatextSimpleDateFormat;用来输出问本格式的

DateFormat //应该是在javautil;里面吧应该是的

你那个错误是编译就没通过啊

public class Test

那你那个编译写的因该是

javac Testjava 编译的应该是类啊而不是javac timejava 请问你的time什么意思呢,所以你报的异常是找不到time类啊

呵呵你是初学java吧该答的我都答完了拉!还特地帮你每句都写了解释

接下来你要给我分了吧

把我上面的那个代码复制进去就OK拉

还有不懂的就补充问题啊

我会关注你的问题的!

Systemoutprintln(new Date());// 设置之前的 虚拟机时间

TimeZonesetDefault(TimeZonegetTimeZone("GMT+8")); //解决虚拟机时间与当前时间的差距

//"GMT+8" 是时区延后8小时

//是否需要增减,得先看下虚拟机时间对不对,正确的话就不需要增减了

//如果虚拟机时间本来就对,就算设置了也是不影响的,可以自己测

Date time = new Date();

Systemoutprintln(time);//设置之后的虚拟机时间

Systemoutprintln(timegetYear()+1900);

Systemoutprintln(timegetMonth()+1);

Systemoutprintln(timegetDate());

Systemoutprintln(timegetHours());

Systemoutprintln(timegetMinutes());

Systemoutprintln(timegetSeconds());

这些方法都是已经被java废弃掉的,现在java推荐的时间 *** 作类是Calendar,这个是必须要跟上时代步伐的,建议去研究研究。

引入 javautil;

Calendar now= CalendargetInstance();//这个就是获取时间的

引入 javatext;

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//这个是用来格式化时间的

sdfformat(nowgetTime()); //返回一个字符串

Systemoutprintln(sdfformat(nowgetTime());//打印出来看看

import javatextSimpleDateFormat;

import javautilDate;

Date d=new Date();//获取时间

SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");//转换格式

Systemoutprintln(sdfformat(d));//打印

以上就是关于Java 如何显示当前系统日期与时间全部的内容,包括:Java 如何显示当前系统日期与时间、如何用java程序获取取internet标准时间、java 获取本机当前时间并对小时任意加减,后判断新时间与当前时间的前后关系 求程序等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/9647513.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-30
下一篇2023-04-30

发表评论

登录后才能评论

评论列表(0条)

    保存