Java Android如何获得明天';日期
在我的android应用程序中。我需要显示明天的日期,例如今天是3月5日,所以我需要显示为3月6日。我知道获取今天日期、月份和年份的代码 日期计算Java Android如何获得明天';日期,java,android,calendar,Java,Android,Calendar,在我的android应用程序中。我需要显示明天的日期,例如今天是3月5日,所以我需要显示为3月6日。我知道获取今天日期、月份和年份的代码 日期计算 GregorianCalendar gc = new GregorianCalendar(); yearat = gc.get(Calendar.YEAR); yearstr = Integer.toString(yearat); monthat = gc.get(Calendar.MONTH) + 1; mo
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);
如果我有密码
dayat = gc.get(Calendar.DAY_OF_MONTH) + 1;
它会显示明天的日期吗。还是在今天的日期上加一个?例如,如果今天是1月31日。使用上面的代码,它会显示为1还是32?如果显示32,我需要做什么更改
日历的形式获取今天的日期
GregorianCalendar gc = new GregorianCalendar();
gc.add(Calendar.DATE, 1);
// now do something with the calendar
像这样试试
dayat = gc.add(Calendar.DATE, 1);
您只需在日历日中添加1
GregorianCalendar gc = new GregorianCalendar();
gc.add(Calendar.DATE, 1);
使用以下代码显示明天的日期
Calendar calendar = Calendar.getInstance();
Date today = calendar.getTime();
calendar.add(Calendar.DAY_OF_YEAR, 1);
Date tomorrow = calendar.getTime();
使用SimpleDataFormat将日期格式化为字符串:
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);
印刷品:
05-Mar-2014
06-Mar-2014
第一个答案几乎涵盖了各种可能性。 但这里有另一个可以从org.apache.commons.lang.time使用的解决方案:
Date lTomorrow = DateUtils.addDays(new Date(), 1);
java.util.Date
和java.util.Calendar
是很难处理的。我建议您使用JodaTime,它有一个更干净/更好的API。现在JodaTime相当标准
请注意,JDK 8将引入一个受JodaTime影响很大的新日期/时间API
- java.util.Date和.Calendar类是出了名的麻烦。避开它们。相反,请使用library或与捆绑的new in
一些使用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 );
使用日历获取今天的日期,然后在其中添加一天。其他选项:
Calendar tomorrow = Calendar.getInstance();
tomorrow.roll(Calendar.DATE, true);
或
roll
也可以通过传递负数来回溯时间,例如:
Calendar yesterday = Calendar.getInstance();
yesterday.roll(Calendar.DATE, -1);
tl;博士
java.time
所有其他答案都过时了,使用了麻烦的旧日期
&日历
类或Joda时间项目(现在处于维护模式)。现代方法使用java.time类
LocalDate
该类表示一个仅限日期的值,不包含一天中的时间和时区
时区对于确定日期至关重要。在任何一个特定的时刻,世界各地的日期都因地区而异。例如,中午夜后几分钟是新的一天,而中仍然是“昨天”
如果未指定时区,JVM将隐式应用其当前默认时区。该默认值可能随时更改,因此您的结果可能会有所不同。最好将所需/预期时区明确指定为参数
以大陆/地区
的格式指定,例如,或太平洋/奥克兰
。切勿使用3-4个字母的缩写,如EST
或IST
,因为它们不是真正的时区,也不是标准化的,甚至不是唯一的(!)
从这个LocalDate
你可以算出第二天的时间
LocalDate tomorrow = today.plusDays( 1 ) ;
串
要生成表示LocalDate
对象值的字符串,请调用toString
以获取按照标准格式化的文本:YYYY-MM-DD
要生成其他格式的字符串,请搜索堆栈溢出
关于java.time 该框架内置于Java8及更高版本中。这些类取代了麻烦的旧日期时间类,例如,& 该项目现已启动,建议迁移到类 要了解更多信息,请参阅。并搜索堆栈溢出以获得许多示例和解释。规格是 从哪里获得java.time类
- ,及以后
- 内置的李>
- 标准JavaAPI的一部分,带有捆绑实现
- Java9添加了一些次要功能和修复
- 及
- 大部分java.time功能都在中向后移植到Java6和Java7
-
- 更高版本的Android捆绑包实现了java.time类
- 对于早期的Android,该项目采用了ThreeTen Backport(如上所述)。看
- 第二天设置的最佳方法是
public void NextDate()
{
final Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
// set current date into textview
e_date.setText(new StringBuilder()
// Month is 0 based, just add 1
.append(mDay+1).append("-").append(mMonth + 1).append("-")
.append(mYear).append(" "));
}
这对我很有用
Date currentDate = new Date();// get the current date
currentDate.setDate(currentDate.getDate() + 1);//add one day to the current date
dateView.setText(currentDate.toString().substring(0, 10));// put the string in specific format in my textView
祝你好运 String lastDate=“2018年5月28日”;
Just call this method and send date from which you want next date
public String nextDate(Date dateClicked) {
//
String next_day;
calander_view.setCurrentDayTextColor(context.getResources().getColor(R.color.white_color));
//calander_view.setCurrentDayBackgroundColor(context.getResources().getColor(R.color.gray_color));
SimpleDateFormat dateFormatForDisplaying = new SimpleDateFormat("dd/MM/yyyy", Locale.getDefault());
String date_format = dateFormatForDisplaying.format(dateClicked);
SimpleDateFormat simpleDateformat = new SimpleDateFormat("E"); // the day of the week abbreviated
final Calendar calendar = Calendar.getInstance();
try {
Date date = dateFormatForDisplaying.parse(date_format);
calendar.setTime(date);
calendar.add(Calendar.DATE, 1);
String nex = dateFormatForDisplaying.format(calendar.getTime());
Date d1 = dateFormatForDisplaying.parse(nex);
String day_1 = simpleDateformat.format(d1);
next_day = nex + ", " + day_1;
} catch (ParseException e) {
e.printStackTrace();
}
return next_day;
}
日历=Calendar.getInstance();
字符串[]sDate=lastDate.split(“/”);
calendar.set(Integer.parseInt(sDate[2])、Integer.parseInt(sDate[0])、Integer.parseInt(sDate[1]);
DateFormat DateFormat=新的SimpleDateFormat(“dd MMM yyyy”);
//String todayAsString=dateFormat.format(今天);
对于(inti=1;我要编译它吗?(感谢复制我的brainfart:)@laalto没有模仿你。只是一般认为明天是一天之后。所以试着给出答案。顺便说一句,你的描述要好得多。所以不用担心。我知道你有一个好的和最好的头脑。无论如何谢谢!!它没有编译,也没有真正的帮助。Lalto让我问一件事…如何获得明天的月份。比如从e日历对象设置为明天,get
MONTH
字段值。使用DateFormat DateFormat=new SimpleDataFormat(“dd-MMM-yyyy”,java.util.Locale.getDefault());若要删除警告,此答案仍不清楚Java 8何时会最终出现在Android手机上。请在回答中添加说明选择月份的最后日期是什么?
LocalDate tomorrow = today.plusDays( 1 ) ;
public void NextDate()
{
final Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
// set current date into textview
e_date.setText(new StringBuilder()
// Month is 0 based, just add 1
.append(mDay+1).append("-").append(mMonth + 1).append("-")
.append(mYear).append(" "));
}
Date currentDate = new Date();// get the current date
currentDate.setDate(currentDate.getDate() + 1);//add one day to the current date
dateView.setText(currentDate.toString().substring(0, 10));// put the string in specific format in my textView
Just call this method and send date from which you want next date
public String nextDate(Date dateClicked) {
//
String next_day;
calander_view.setCurrentDayTextColor(context.getResources().getColor(R.color.white_color));
//calander_view.setCurrentDayBackgroundColor(context.getResources().getColor(R.color.gray_color));
SimpleDateFormat dateFormatForDisplaying = new SimpleDateFormat("dd/MM/yyyy", Locale.getDefault());
String date_format = dateFormatForDisplaying.format(dateClicked);
SimpleDateFormat simpleDateformat = new SimpleDateFormat("E"); // the day of the week abbreviated
final Calendar calendar = Calendar.getInstance();
try {
Date date = dateFormatForDisplaying.parse(date_format);
calendar.setTime(date);
calendar.add(Calendar.DATE, 1);
String nex = dateFormatForDisplaying.format(calendar.getTime());
Date d1 = dateFormatForDisplaying.parse(nex);
String day_1 = simpleDateformat.format(d1);
next_day = nex + ", " + day_1;
} catch (ParseException e) {
e.printStackTrace();
}
return next_day;
}
String lastDate="5/28/2018";
Calendar calendar = Calendar.getInstance();
String[] sDate=lastDate.split("/");
calendar.set(Integer.parseInt(sDate[2]),Integer.parseInt(sDate[0]),Integer.parseInt(sDate[1]));
DateFormat dateFormat = new SimpleDateFormat("dd-MMM-yyyy");
// String todayAsString = dateFormat.format(today);
for (int i=1;i<29;i++)
{
calendar.add(Calendar.DAY_OF_YEAR,1);
// td[i].setText(dateFormat.format(calendar.getTime()));
System.out.println(dateFormat.format(calendar.getTime()));
}