2

I have a bunch of dates formatted with the year and week, as follows:

2011-10

The week value is the week of the year(so 1-52). From this week value, I need to output something like the following:

Mar 7

Explicitly, I need the Month that the given week is in, and the date of the first Monday of that week. So in other words it is saying that the 10th week of the year is the week of March 7th.

I am using Groovy. What kind of date manipulation can I do to get this to work?

Yottagray
  • 2,552
  • 5
  • 32
  • 43
  • 2
    Please see the answer given by BalusC here http://stackoverflow.com/questions/2109145/how-to-get-first-day-of-a-given-week-number-in-java – Sean Mar 11 '11 at 19:15
  • Can anyone help with an answer that works in Groovy? – Yottagray Mar 11 '11 at 20:49
  • Doesn't `new java.text.SimpleDateFormat('yyyy-w').parse('2011-10')` work? If not try setting the Locale. – Adam Mar 11 '11 at 21:01
  • Can you use the Joda date-time library? It is a Java library and so will work with Groovy just fine. – Michael Easter Mar 12 '11 at 04:24

4 Answers4

1

Use a GregorianCalendar (or Joda, if you don't mind a dependency)

    String date = "2011-10";
    String[] parts = date.split("-");
    Calendar cal = Calendar.getInstance();
    cal.set(Calendar.YEAR, Integer.parseInt(parts[0]));
    cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
    cal.set(Calendar.WEEK_OF_YEAR, Integer.parseInt(parts[1])+1);
    DateFormat df = new SimpleDateFormat("MMM d");
    System.out.println(df.format(cal.getTime()) + " (" + cal.getTime() + ")");

EDIT: Added +1 to week, since calendar uses zero-based week numbers

Sam Barnum
  • 10,559
  • 3
  • 54
  • 60
  • Thank you for your help. This is what I have so far, based on your answer, but it keeps giving me errors on the ` – Yottagray Mar 11 '11 at 20:08
1

You can use SimpleDateFormat, just like in java. See groovyconsole.appspot.com/script/439001

java.text.DateFormat df = new java.text.SimpleDateFormat('yyyy-w', new Locale('yourlocale'))
Date date = df.parse('2011-10')

To add a week, simply use Date date = df.parse('2011-10')+7

You don't need to set the Locale if your default Locale is using Monday as the first day of week.

Adam
  • 5,045
  • 1
  • 25
  • 31
  • I don't believe this quite works - it gives you the Sunday of the week rather than the Monday, which the OP is asking for. It may also be a week off (see Sam's comment re: zero-based week numbers). – Rob Hruska Mar 11 '11 at 19:14
  • You can set your locale in the dateformat (updated the answer). – Adam Mar 11 '11 at 19:26
  • Can the Monday be retrieved instead of Sunday by simply changing the locale? If so, can you update your example to reflect how one might do that? I only ask, because a simple copy-and-paste of your code to an en_US locale doesn't quite meet the requirements of the question. – Rob Hruska Mar 11 '11 at 19:33
  • http://groovyconsole.appspot.com/script/439001 . Click 'edit in console' -> 'execute script', result displayed below editor. – Adam Mar 11 '11 at 19:36
  • I've removed my downvote. It might not hurt to update your answer to indicate that the day of week returned is dependent on the locale that's provided to the SimpleDateFormat instance. – Rob Hruska Mar 11 '11 at 19:46
  • @Rob Hruska: SimpleDateFormat(String pattern) uses the default Locale, but I've updated the answer just to be clear. – Adam Mar 11 '11 at 19:51
1
Date date = new SimpleDateFormat("yyyy-w", Locale.UK).parse("2011-10");
System.out.println(new SimpleDateFormat("MMM d").format(date));

The first line returns first day of the 10th week in British Locale (March 7th). When Locale is not enforced, the results are dependent on default JVM Locale.

Formats are explained here.

Tomasz Nurkiewicz
  • 334,321
  • 69
  • 703
  • 674
1

Here's a groovy solution:

use(groovy.time.TimeCategory) {
    def (y, w) = "2011-10".tokenize("-")
    w = ((w as int) + 1) as String
    def d = Date.parse("yyyy-w", "$y-$w") + 1.day
    println d.format("MMM dd")
}
ataylor
  • 64,891
  • 24
  • 161
  • 189