Java Android 如何获取明天的日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22195093/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android how to get tomorrow's date
提问by Jocheved
In my android application. I need to display tomorrow's date, for example today is 5th March so I need to display as 6 March. I know the code for getting today's date, month and year.
在我的安卓应用程序中。我需要显示明天的日期,例如今天是 3 月 5 日,所以我需要显示为 3 月 6 日。我知道获取今天的日期、月份和年份的代码。
date calculating
日期计算
GregorianCalendar gc = new GregorianCalendar();
yearat = gc.get(Calendar.YEAR);
yearstr = Integer.toString(yearat);
monthat = gc.get(Calendar.MONTH) + 1;
monthstr = Integer.toString(monthat);
dayat = gc.get(Calendar.DAY_OF_MONTH);
daystr = Integer.toString(dayat);
If I have the code
如果我有代码
dayat = gc.get(Calendar.DAY_OF_MONTH) + 1;
will it display tomorrow's date. or just add one to today's date? For example, if today is January 31. With the above code, will it display like 1 or 32? If it displays 32, what change I need to make?
它会显示明天的日期。或者只是在今天的日期上加一个?比如今天是1月31日,上面的代码,会显示像1还是32?如果显示 32,我需要做哪些更改?
采纳答案by laalto
Get today's date as a
Calendar
.Add 1 day to it.
Format for display purposes.
获取今天的日期作为
Calendar
.加1天。
用于显示目的的格式。
For example,
例如,
GregorianCalendar gc = new GregorianCalendar();
gc.add(Calendar.DATE, 1);
// now do something with the calendar
回答by Naveen
Try like this..
试试这样..
dayat = gc.add(Calendar.DATE, 1);
回答by Pararth
Calendar calendar = Calendar.getInstance();
Date today = calendar.getTime();
calendar.add(Calendar.DAY_OF_YEAR, 1);
Date tomorrow = calendar.getTime();
回答by Piyush
you have to add just 1 in your Calendar Day.
您只需在日历日添加 1。
GregorianCalendar gc = new GregorianCalendar();
gc.add(Calendar.DATE, 1);
回答by NaVaNeeTh PrOdHutuR
Use the following code to display tomorrow date
使用以下代码显示明天的日期
Calendar calendar = Calendar.getInstance();
Date today = calendar.getTime();
calendar.add(Calendar.DAY_OF_YEAR, 1);
Date tomorrow = calendar.getTime();
Use SimpleDateFormat to format the Date as a String:
使用 SimpleDateFormat 将日期格式化为字符串:
DateFormat dateFormat = new SimpleDateFormat("dd-MMM-yyyy");
String todayAsString = dateFormat.format(today);
String tomorrowAsString = dateFormat.format(tomorrow);
System.out.println(todayAsString);
System.out.println(tomorrowAsString);
Prints:
印刷:
05-Mar-2014
06-Mar-2014
回答by Stefan Beike
the first answers pretty much covers the possibilities. but here one another solution which you can use from org.apache.commons.lang.time:
第一个答案几乎涵盖了可能性。但这里有另一种解决方案,您可以从 org.apache.commons.lang.time 使用它:
Date lTomorrow = DateUtils.addDays(new Date(), 1);
回答by lance-java
java.util.Date
and java.util.Calendar
are terrible to work with. I suggest you use JodaTime which has a much cleaner / nicer API. JodaTime is pretty standard these days.
java.util.Date
和java.util.Calendar
他们一起工作很糟糕。我建议你使用 JodaTime,它有一个更干净/更好的 API。JodaTime 现在非常标准。
http://www.joda.org/joda-time/#Why_Joda-Time
http://www.joda.org/joda-time/#Why_Joda-Time
Note that JDK 8 will introduce a new date/time API heavily influenced by JodaTime.
请注意,JDK 8 将引入一个受 JodaTime 影响很大的新日期/时间 API。
回答by Basil Bourque
The java.util.Date and .Calendar classes are notoriously troublesome. Avoid them. Instead use either Joda-Timelibrary or the new java.time packagein bundled with Java 8.
java.util.Date 和 .Calendar 类是出了名的麻烦。避开它们。而是使用Joda-Time库或与Java 8捆绑在一起的新java.time 包。
Some example code using the Joda-Time 2.3 library.
使用 Joda-Time 2.3 库的一些示例代码。
DateTimeZone timeZone = DateTimeZone.forID( "Europe/Paris" );
DateTime now = new DateTime( timeZone );
DateTime tomorrow = now.plusDays( 1 );
String output = DateTimeFormat.forPattern( "FF" ).withLocale(Locale.FRANCE).print( tomorrow );
回答by roshanpeter
Get todays date by using calendar and then add 1 day to it.
使用日历获取今天的日期,然后添加 1 天。
回答by Alberto Malagoli
Other options:
其他选项:
Calendar tomorrow = Calendar.getInstance();
tomorrow.roll(Calendar.DATE, true);
or
或者
tomorrow.roll(Calendar.DATE, 1);
roll
can also be used to go back in time by passing a negative number, so for example:
roll
也可用于通过传递负数来回到过去,例如:
Calendar yesterday = Calendar.getInstance();
yesterday.roll(Calendar.DATE, -1);