1

In Java (I'm a junior) - how do you perform various date operations?

Assess "storedDate is one week away from today" and "storedDate has passed"

I am not sure if its the best approach or how to do the condition check

java.sql.Date  dueDate = (Date) loggedUser.get("dueDate");

I've seen various calculations like this - but not sure an if condition could be met like this?

Calendar c= Calendar.getInstance();
//c.set(Calendar.DAY_OF_WEEK,Calendar.SUNDAY);
c.set(Calendar.HOUR_OF_DAY,0);
c.set(Calendar.MINUTE,0);
c.set(Calendar.SECOND,0);
DateFormat df=new SimpleDateFormat("EEE yyyy/MM/dd HH:mm:ss");
System.out.println(df.format(c.getTime()));// This past Sunday [ May include today ]
c.add(Calendar.DATE,7);
System.out.println(df.format(c.getTime()));// Next Sunday
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • 1
    You should check Joda Time, or native new Java time APi if you're using java 8 if you're doing this for java side. If you're trying to make a SQL Query, it's not appropriate... You should provide more details. – Mateo Barahona Sep 21 '17 at 19:33
  • 1
    I recomment moving on to java.time, the new time/date API with java 8. It is more sophisticated and simpler – Houssam Badri Sep 21 '17 at 19:34
  • @HoussemBdr - when you say Java 8 - you mean to upgrade my Tomcat from 7 to 8? -- What is Joda Time - a new depencie? -@Mateo Barahona –  Sep 21 '17 at 19:44
  • @Rob : http://www.joda.org/joda-time/ You should explain what you're trying to achieve, i'm not sure you're going the right way – Mateo Barahona Sep 21 '17 at 19:47
  • 1
    @Rob No, it means to use the Java 8 classes for handling dates. Joda time is an alternative API for dates in Java. – UninformedUser Sep 21 '17 at 19:47
  • 2
    Rob, this is not the first time I had to remove [java-ee] tag from your question. Please take that as a hint to pay extra attention to your tagging. General rule is, if your problem is demonstrable using a plain vanilla Java application class with `main()` method, then it's very definitely not [java-ee] related. – BalusC Sep 21 '17 at 20:19
  • Well I didn't know that @BalusC - I tried to tag it J2EE - if I wasn't doing UX, Design, Frontend and Backend development I would have made more of an effort to notice your edit hints. –  Sep 21 '17 at 22:02
  • J2EE has been renamed to Java EE since 2006. More than a decade ago. – BalusC Sep 21 '17 at 22:09
  • 1
    Sure ok I'll take note –  Sep 21 '17 at 22:13
  • FYI, both the `Calendar` class and the Joda-Time project are now outmoded by the [*java.time*](https://docs.oracle.com/javase/9/docs/api/java/time/package-summary.html) classes. – Basil Bourque Jan 14 '18 at 00:17

2 Answers2

0

Step 1: Learn to read API pages. Here is the Calendar API Page, read it.

For your class project, you don't care about timezone (since you didn't mention it in your post).

Get a date parameter: public void methodName(Date dateParameter)

Create a Calendar containing now: Calendar myCalendar = Calendar.getInstance();

Set a calendar to a date value: myCalendar.setTime(dateParameter)

"Calculate" one week from today: myCalendar.roll(Calendar.DAY_OF_YEAR, 7)

The Calendar object stores Millisecond, Second, Minute, and Hour. Clear these using the set method.

Compare Calendar objects using the after method.

DwB
  • 37,124
  • 11
  • 56
  • 82
  • "Compare Calendar objects using the after method." - please elaborate --- like a .equals? –  Sep 21 '17 at 19:46
  • Read the Calendar api page. – DwB Sep 21 '17 at 20:01
  • The troublesome `Calendar` class has been outmoded for years, supplanted by the *java.time* classes built into Java 8 and later. For Java 6 & 7, see the *ThreeTen-Backport* project. – Basil Bourque Jan 14 '18 at 00:08
0

tl;dr

LocalDate.now( ZoneId.of( "Pacific/Auckland" ) )
.with( 
    TemporalAdjusters.previousOrSame​( DayOfWeek.SUNDAY ) 
)

java.time

The modern approach uses java.time classes.

Get current moment in UTC.

Instant instant = Instant.now() ; 

If you care about day-of-week, you care about date. Determining a date requires a time zone. For any given moment the date varies around the globe by zone.

ZoneId z = ZoneId.of( "Africa/Tunis" ) ;
ZonedDateTime zdt = instant.atZone( z ) ;  // Same point on the timeline, different wall-clock time.

To focus on date-only without time-of-day, extract a LocalDate.

LocalDate ld = zdt.toLocalDate() ;

To adjust into other moments, use a TemporalAdjuster. Find implementations in TemporalAdjusters.

LocalDate previousOrSameSunday = ld.with( 
    TemporalAdjusters.previousOrSame​( DayOfWeek.SUNDAY ) 
) ;

…and…

LocalDate nextOrSameSunday = ld.with( 
    TemporalAdjusters.nextOrSame​( DayOfWeek.SUNDAY ) 
) ;

To compare, look for isBefore, isAfter, isEqual, and equals methods on the various java.time classes.

thisLocalDate.isBefore( thatLocalDate) 

About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

Where to obtain the java.time classes?

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154