Skip to content

Instantly share code, notes, and snippets.

@pstoellberger
Created March 18, 2013 14:55
Show Gist options
  • Save pstoellberger/5187724 to your computer and use it in GitHub Desktop.
Save pstoellberger/5187724 to your computer and use it in GitHub Desktop.
datestuff
package org.saiku;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class Datestuff {
public static void main(String[] args) throws Exception {
String date = "201210";
String format = "MONTHLY";
Calendar c = Calendar.getInstance();
if ("WEEKLY".equals(format.toUpperCase())) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyww");
Date d = sdf.parse(date);
c.setTime(d);
System.out.println("period_this_to: " + sdf.format(c.getTime()));
c.add(Calendar.WEEK_OF_YEAR, -51);
System.out.println("period_this_from: " + sdf.format(c.getTime()));
c.add(Calendar.WEEK_OF_YEAR, -1);
System.out.println("period_last_to: " + sdf.format(c.getTime()));
c.add(Calendar.WEEK_OF_YEAR, -51);
System.out.println("period_last_from: " + sdf.format(c.getTime()));
}
if ("MONTHLY".equals(format.toUpperCase())) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
Date d = sdf.parse(date);
c.setTime(d);
System.out.println("period_this_to: " + sdf.format(c.getTime()));
c.add(Calendar.MONTH, -11);
System.out.println("period_this_from: " + sdf.format(c.getTime()));
c.add(Calendar.MONTH, -1);
System.out.println("period_last_to: " + sdf.format(c.getTime()));
c.add(Calendar.MONTH, -11);
System.out.println("period_last_from: " + sdf.format(c.getTime()));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment