Created
March 18, 2013 14:55
-
-
Save pstoellberger/5187724 to your computer and use it in GitHub Desktop.
datestuff
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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