例如:
Calendar c = Calendar.getInstance();
DateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
c.setTime( sdf.parse("31/12/2010"));
out.println( c.get( Calendar.WEEK_OF_YEAR ) );
Run Code Online (Sandbox Code Playgroud)
打印1
Joda时间也是如此.
:)
我知道一年中的周数,一周从星期日开始,然后是星期一,星期二......星期六.
既然我知道周数,那么通过使用Java代码获取特定周的日期的有效方法是什么?
如何约会一年中的哪一周?我尝试了以下代码:
Calendar sDateCalendar = new GregorianCalendar();
sDateCalendar.set(Integer.parseInt(sDateYearAAAA), Integer.parseInt(sDateMonthMM)-1, Integer.parseInt(sDateDayDD));
System.out.format("sDateCalendar %tc\n", sDateCalendar);
iStartWeek = sDateCalendar.getWeekYear();
System.out.println("iStartWeek "+iStartWeek+ " "+sDateCalendar.WEEK_OF_YEAR);
Run Code Online (Sandbox Code Playgroud)
我获得:sDateCalendar lun apr 23 11:58:39 CEST 2012 iStartWeek 2012 3
而一年中正确的一周是17.有人可以帮助我吗?
我在SimpleDateFormat上得到了一些令人费解的结果,我希望有人可以对这个问题有所了解.输出:
Time = Mon Dec 27 00:00:00 PST 2010
2010-01 <--- THIS IS WHAT I DON'T UNDERSTAND
Start of week = Sun Dec 26 00:00:00 PST 2010
2010-01
End of Week = Sat Jan 01 23:59:59 PST 2011
2011-01
Run Code Online (Sandbox Code Playgroud)
作为特例,我应该将今年的最后一周视为延续到明年吗?或者这是解释这个的正确方法吗?显然,当尝试按顺序组织周时,订单不正确.调整初始值,2005年12月25日被认为是第53周.我还没有看过Joda,看看Joda是否会产生类似的结果.
相关代码:
private static Date getStartOfWeek( Date d ) {
Calendar calendar = Calendar.getInstance();
calendar.clear();
calendar.setTime( d );
calendar.set( Calendar.DAY_OF_WEEK, calendar.getFirstDayOfWeek() );
return calendar.getTime();
}
private static Date getEndOfWeek( Date d ) {
Calendar calendar = Calendar.getInstance();
calendar.clear();
calendar.setTime( d ); …Run Code Online (Sandbox Code Playgroud) 我有一个约会,如何让所有日期落在给定日期属于java的那一周?
example:
if i give today's date then i should get all dates belonging to this week.
12 July 2015 to 18 July 2015
Run Code Online (Sandbox Code Playgroud)
请有人帮助我.
我想知道给定日期的星期日期,我已解释为什么这个问题不重复,请在评论前阅读.
我正在制作一个令我困惑的项目.
给定是List<TimeInterval> list包含类的元素,TimeInterval如下所示:
public class TimeInterval {
private static final Instant CONSTANT = new Instant(0);
private final LocalDate validFrom;
private final LocalDate validTo;
public TimeInterval(LocalDate validFrom, LocalDate validTo) {
this.validFrom = validFrom;
this.validTo = validTo;
}
public boolean isValid() {
try {
return toInterval() != null;
}
catch (IllegalArgumentException e) {
return false;
}
}
public boolean overlapsWith(TimeInterval timeInterval) {
return this.toInterval().overlaps(timeInterval.toInterval());
}
private Interval toInterval() throws IllegalArgumentException {
return new Interval(validFrom.toDateTime(CONSTANT), validTo.toDateTime(CONSTANT));
}
Run Code Online (Sandbox Code Playgroud)
使用以下内容生成间隔:
TimeInterval …Run Code Online (Sandbox Code Playgroud) 我正在使用Java 6和SQL Server 2008编写报表系统.对于某些查询,我想按周编号查看数据.我正在使用Java来填补数据中的空白以形成连续的时间线,我发现了这一点
java.util.Calendar cal = new java.util.GregorianCalendar();
cal.set(2012, 0, 1);
cal.get(Calendar.WEEK_OF_YEAR);
Run Code Online (Sandbox Code Playgroud)
和
org.joda.time.DateTime date = new org.joda.time.DateTime(2012, 01, 01, 0, 0);
date.getWeekOfWeekyear();
Run Code Online (Sandbox Code Playgroud)
返回不同的周数
DATEPART(WEEK, '2012-01-01')
Run Code Online (Sandbox Code Playgroud)
有没有办法解决这个差异,还是我必须选择使用SQL Server 或 Java周数?
TIA
我有一个Java日期:tempDate = Sun Jan 01 00:00:00 GMT + 01:00 2012.
我想创建一个新的日期,即tempDate年份第一周的第一天.
那是:
Mon Jan 02 00:00:00 GMT+01:00 2012
Run Code Online (Sandbox Code Playgroud)
当我尝试使用Joda时间时:
DateTime cal = new DateTime(tempDate).withWeekOfWeekyear(1).
withDayOfWeek(DateTimeConstants.MONDAY);
Run Code Online (Sandbox Code Playgroud)
我得到了我想要的,但在前一年:
Mon Jan 03 00:00:00 GMT+01:00 2011
Run Code Online (Sandbox Code Playgroud)
我如何在Joda Time中完成这项工作?
如果可能的话,我希望joda或非joda解决方案适用于下面的场景
让我们说如果我的一周从02/05/2012开始,并且给定的当前日期是02/22/2011.我需要计算给定当前日期的周开始日期和结束日期.因此,我的解决方案应该在02/19开始一周,在02/25结束一周.为简单起见,我已将我的周开始时间设置为02/05/2011,但它可能是任何一天,我的周总有7天.
我现有的代码如下,但似乎没有按预期工作.
public Interval getWeekInterval(Date calendarStartDate, Date date)
{
Calendar sDate = Calendar.getInstance();
sDate.setTime(getMidnightDate(calendarStartDate));
Calendar eDate = Calendar.getInstance();
eDate.setTime(date);
Calendar weekStartDate = (Calendar) sDate.clone();
logger.debug("Date:" + sDate.getTime());
while (sDate.before(eDate)) {
weekStartDate = sDate;
sDate.add(Calendar.DAY_OF_WEEK_IN_MONTH, 1);
}
return new Interval(weekStartDate.getTime(), sDate.getTime());
}
Run Code Online (Sandbox Code Playgroud)