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?

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

4 Answers 4

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")
}
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

1
  • Thank you for your help. This is what I have so far, based on your answer, but it keeps giving me errors on the <tb any ideas what I might be doing wrong? date = "2011-10" parts = date.split("-") cal = new GregorianCalendar() 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) cal.time println String.format('%<tb %<te', cal)
    – Yottagray
    Mar 11, 2011 at 20:08
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.

4
  • Like Adam's answer, this returns Sunday instead of Monday.
    – Rob Hruska
    Mar 11, 2011 at 19:20
  • Strange, it prints mar 7 on my computer? Mar 11, 2011 at 19:41
  • It seems it's dependent on the Locale being used. If you update your answer, I can remove my downvote (I can't remove it unless an edit is performed).
    – Rob Hruska
    Mar 11, 2011 at 19:45
  • Thanks, I enforced Locale to prevent system-dependent behavior. Mar 11, 2011 at 19:59
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.

6
  • 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, 2011 at 19:14
  • You can set your locale in the dateformat (updated the answer).
    – Adam
    Mar 11, 2011 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, 2011 at 19:33
  • groovyconsole.appspot.com/script/439001 . Click 'edit in console' -> 'execute script', result displayed below editor.
    – Adam
    Mar 11, 2011 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, 2011 at 19:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.