在特定日期后下星期一获得第一? [英] Get first next Monday after certain date?
问题描述
我知道这里有同样的问题,但我试过答案提供了,它返回一个我不明白的输出。我感到困惑的答案,我不认为输出是正确的。
我需要帮助,谢谢:)
GregorianCalendar date1 = new GregorianCalendar(2014,05,12); // 05是月6月开始从0 -11
while(date1.get(Calendar.DAY_OF_WEEK)!= Calendar.MONDAY)
date1.add(Calendar.DATE,1) ;
System.out.println(date1);
这是输出:
java.util.GregorianCalendar [time = 1405267200000,areFieldsSet = true,areAllFieldsSet = true,lenient = true,zone = sun.util.calendar.ZoneInfo [id =Asia / Singapore 28800000,dstSavings = 0,useDaylight = false,transitions = 9,lastRule = null],firstDayOfWeek = 1,minimalDaysInFirstWeek = 1,ERA = 1,YEAR = 2014,MONTH = 6,WEEK_OF_YEAR = 29,WEEK_OF_MONTH = 3,DAY_OF_MONTH = 14 ,DAY_OF_YEAR = 195,DAY_OF_WEEK = 2,DAY_OF_WEEK_IN_MONTH = 2,AM_PM = 0,HOUR = 0,HOUR_OF_DAY = 0,MINUTE = 0,SECOND = 0,MILLISECOND = 0,ZONE_OFFSET = 28800000,DST_OFFSET = 0]
我应该提取哪一个输出以检索星期一的日期?
而不是 System.out.println(date1);
使用 System.out.println(date1。 getTime());
getTime
返回 Date
,代表日历
的当前状态c $ c> Mon Jul 14 00:00:00 EST 2014
System.out.println(date1)
是使用 System.out.println(date1.toString())
的等价,在这种情况下,是倾销一堆有用的关于日历
对象的状态的信息,但不是真正的人类可读数据。
System.out.println(date1.getTime())
将使用日期
的 toString
方法显示日期值,根据当前的本地设置进行格式化,这将提供更多有用的信息。
更新 p>
不要使用 GregorianCalendar
,应使用系统 Calendar
,例如...
日历date1 = Calendar.getInstance
date1.set(2014,06,12);
此外,月份 0
Janurary实际上是 0
不是 1
,因此在您的示例中,您指定的月份为7月,
而是使用...
= Calendar.getInstance();
date1.set(2014,05,12);
while(date1.get(Calendar.DAY_OF_WEEK)!= Calendar.MONDAY){
date1.add(Calendar.DATE,1);
}
System.out.println(date1.getTime());
哪个输出...
Mon Jun 16 16:22:26 EST 2014
星期一从今天...或多或少;)
I know there is the same question here, but I have tried the answer provided and it returned an output that I don't understand. I am confused by the answer and I don't think the output is correct.
I need help, thank you :)
GregorianCalendar date1 = new GregorianCalendar( 2014, 05, 12 ); //05 is june as month start from 0 -11
while( date1.get( Calendar.DAY_OF_WEEK ) != Calendar.MONDAY )
date1.add( Calendar.DATE, 1 );
System.out.println(date1);
Here is the output:
java.util.GregorianCalendar[time=1405267200000,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="Asia/Singapore",offset=28800000,dstSavings=0,useDaylight=false,transitions=9,lastRule=null],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2014,MONTH=6,WEEK_OF_YEAR=29,WEEK_OF_MONTH=3,DAY_OF_MONTH=14,DAY_OF_YEAR=195,DAY_OF_WEEK=2,DAY_OF_WEEK_IN_MONTH=2,AM_PM=0,HOUR=0,HOUR_OF_DAY=0,MINUTE=0,SECOND=0,MILLISECOND=0,ZONE_OFFSET=28800000,DST_OFFSET=0]
Where on the output should I extract to retrieve Monday's date?
Instead of System.out.println(date1);
use System.out.println(date1.getTime());
getTime
returns an instance of Date
which represents the current state of the Calendar
Which will output Mon Jul 14 00:00:00 EST 2014
System.out.println(date1)
is the equivlent of using System.out.println(date1.toString())
, which, in this case, is dumping a bunch of useful info about the state of the Calendar
object, but not really human readable data.
System.out.println(date1.getTime())
will use the Date
's to toString
method to display a date value, formatted based on the current local settings, which will provide more useful information.
Updated
Instead of using GregorianCalendar
, you should use the system Calendar
, for example...
Calendar date1 = Calendar.getInstance();
date1.set(2014, 06, 12);
Also, months are 0
indexed, meaning that Janurary is actually 0
not 1
, so in your example, you've specified the month as July, not June.
So, instead, using...
Calendar date1 = Calendar.getInstance();
date1.set(2014, 05, 12);
while (date1.get(Calendar.DAY_OF_WEEK) != Calendar.MONDAY) {
date1.add(Calendar.DATE, 1);
}
System.out.println(date1.getTime());
Which outputted...
Mon Jun 16 16:22:26 EST 2014
Which is next Monday from today...more or less ;)
这篇关于在特定日期后下星期一获得第一?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!