永发信息网

如何获取明天的日期

答案:2  悬赏:60  手机版
解决时间 2021-04-14 18:59
如何获取明天的日期
最佳答案
有两种方法:
方法一:用java.util.Date类来实现,并结合java.text.DateFormat类来实现时间的格式化,看下面代码:
import java.util.*;
import java.text.*;
//以下默认时间日期显示方式都是汉语语言方式
//一般语言就默认汉语就可以了,时间日期的格式默认为MEDIUM风格,比如:2008-6-16 20:54:53
//以下显示的日期时间都是再Date类的基础上的来的,还可以利用Calendar类来实现见类TestDate2.java
public class TestDate {
public static void main(String[] args) {
Date now = new Date();
Calendar cal = Calendar.getInstance();

DateFormat d1 = DateFormat.getDateInstance(); //默认语言(汉语)下的默认风格(MEDIUM风格,比如:2008-6-16 20:54:53)
String str1 = d1.format(now);
DateFormat d2 = DateFormat.getDateTimeInstance();
String str2 = d2.format(now);
DateFormat d3 = DateFormat.getTimeInstance();
String str3 = d3.format(now);
DateFormat d4 = DateFormat.getInstance(); //使用SHORT风格显示日期和时间
String str4 = d4.format(now);
DateFormat d5 = DateFormat.getDateTimeInstance(DateFormat.FULL,DateFormat.FULL); //显示日期,周,时间(精确到秒)
String str5 = d5.format(now);
DateFormat d6 = DateFormat.getDateTimeInstance(DateFormat.LONG,DateFormat.LONG); //显示日期。时间(精确到秒)
String str6 = d6.format(now);
DateFormat d7 = DateFormat.getDateTimeInstance(DateFormat.SHORT,DateFormat.SHORT); //显示日期,时间(精确到分)
String str7 = d7.format(now);
DateFormat d8 = DateFormat.getDateTimeInstance(DateFormat.MEDIUM,DateFormat.MEDIUM); //显示日期,时间(精确到分)
String str8 = d8.format(now);//与SHORT风格相比,这种方式最好用

System.out.println("用Date方式显示时间: " + now);//此方法显示的结果和Calendar.getInstance().getTime()一样

System.out.println("用DateFormat.getDateInstance()格式化时间后为:" + str1);
System.out.println("用DateFormat.getDateTimeInstance()格式化时间后为:" + str2);
System.out.println("用DateFormat.getTimeInstance()格式化时间后为:" + str3);
System.out.println("用DateFormat.getInstance()格式化时间后为:" + str4);

System.out.println("用DateFormat.getDateTimeInstance(DateFormat.FULL,DateFormat.FULL)格式化时间后为:" + str5);
System.out.println("用DateFormat.getDateTimeInstance(DateFormat.LONG,DateFormat.LONG)格式化时间后为:" + str6);
System.out.println("用DateFormat.getDateTimeInstance(DateFormat.SHORT,DateFormat.SHORT)格式化时间后为:" + str7);
System.out.println("用DateFormat.getDateTimeInstance(DateFormat.MEDIUM,DateFormat.MEDIUM)格式化时间后为:" + str8);
}
}
运行结果:
用Date方式显示时间: Mon Jun 16 20:54:53 CST 2008
用DateFormat.getDateInstance()格式化时间后为:2008-6-16
用DateFormat.getDateTimeInstance()格式化时间后为:2008-6-16 20:54:53
用DateFormat.getTimeInstance()格式化时间后为:20:54:53
用DateFormat.getInstance()格式化时间后为:08-6-16 下午8:54
用DateFormat.getDateTimeInstance(DateFormat.FULL,DateFormat.FULL)格式化时间后为
:2008年6月16日 星期一 下午08时54分53秒 CST
用DateFormat.getDateTimeInstance(DateFormat.LONG,DateFormat.LONG)格式化时间后为
:2008年6月16日 下午08时54分53秒
用DateFormat.getDateTimeInstance(DateFormat.SHORT,DateFormat.SHORT)格式化时间后
为:08-6-16 下午8:54
用DateFormat.getDateTimeInstance(DateFormat.MEDIUM,DateFormat.MEDIUM)格式化时间
后为:2008-6-16 20:54:53

方法二:用java.util.Calendar类来实现,看下面:
import java.util.*;
import java.text.*;
//以下是利用Calendar类来实现日期时间的,和Date类相比较比较简单
public class TestDate2 {
public static void main(String[] args) {

Calendar ca = Calendar.getInstance();
int year = ca.get(Calendar.YEAR);//获取年份
int month=ca.get(Calendar.MONTH);//获取月份
int day=ca.get(Calendar.DATE);//获取日
int minute=ca.get(Calendar.MINUTE);//分
int hour=ca.get(Calendar.HOUR);//小时
int second=ca.get(Calendar.SECOND);//秒
int WeekOfYear = ca.get(Calendar.DAY_OF_WEEK);

System.out.println("用Calendar.getInstance().getTime()方式显示时间: " + ca.getTime());
System.out.println("用Calendar获得日期是:" + year +"年"+ month +"月"+ day + "日");

System.out.println("用Calendar获得时间是:" + hour +"时"+ minute +"分"+ second +"秒");
System.out.println(WeekOfYear);//显示今天是一周的第几天(我做的这个例子正好是周二,故结果显示2,如果你再周6运行,那么显示6)

}
}
运行结果是:
用Calendar.getInstance().getTime()方式显示时间: Mon Jun 16 21:54:21 CST 2008
用Calendar获得日期是:2008年5月16日
用Calendar获得时间是:9时54分21秒
2

总结:中的来说,方法二是最方便的,方法一显得分笨拙,不过看个人喜欢了。
全部回答

错误出现在:today.getmonth()==5 today.getmonth()==7,中间没有||号,还有下面的两个theday=1后面没有“;”(分号),写程序要养成好的习惯!改正后的程序如下:

<script type="text/javascript">  var enabled = 0; today = new date(); var day; var date; var themonth var theday if(today.getmonth()==1||today.getmonth()==3||today.getmonth()==5||today.getmonth()==7||today.getmonth()==8||today.getmonth()==10||today.getmonth()==12){     if(today.getdate()>31){     themonth=today.getdate()+1;theday=1;     } }else{     if(today.getdate()>30){     themonth=today.getdate()+1;theday=1;     } } date1 =(themonth + 1 ) + "月" +(theday+1) + "日 " ; document.write(date1); </script>
结果为:33月2日

我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
看到路边要饭的老人是什么感受?
锡都路这个地址在什么地方,我要处理点事
麦蒂能卷土重来?
求广州hiphop文化店的具体地址
从长安到广州火车站最后一般车是什么时间
QQ家园在哪儿
DNF中除附魔职业还有什么?
单选题下列有关酶的叙述,不正确的是A.所有的
陕西省西安市未央区草滩镇景家基督教堂在哪里
趣事作文开头怎么写
photoshop怎么把修改过的图像保存为JEPG等格
有谁知道单独去广西新华社照相馆照毕业照需要
辗转反侧意思,辗转反侧的侧是念ce还是念zhai
请用化学符号表示:(1)锌原子________.(2
衣服的毛压平了,怎么弄回原样
推荐资讯
东营中凯驾校什么时候考理论?
单选题下列对辩证唯物主义的物质概念理解错误
小新星英语在什么地方啊,我要过去处理事情
威宁有没黑彝
the most and least什么意思?
7角=________元  2.4元=________角  1米6
为什么我的QQ总是自动离线
为什么她一次两次的欺骗我,我要怎么做才好?
甘肃经济频道手机如何回看
顺德区佛山顺德区龙江集北劳动服务站我想知道
大连理工大连海事哪个好
乒乓球在如何在教学中体现人文精神的培养
正方形一边上任一点到这个正方形两条对角线的
阴历怎么看 ?