Skip to content

Instantly share code, notes, and snippets.

@aweijnitz
Created March 25, 2014 20:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aweijnitz/9770743 to your computer and use it in GitHub Desktop.
Save aweijnitz/9770743 to your computer and use it in GitHub Desktop.
Illustration of basic date arithmetics in Java.
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
public class DateAritmethics {
public static void main(String[] args) {
// Subtract three months from today.
//
String dateFormat = "yyyy.MM.dd 'at' HH:mm:ss zzzz";
Date today = new Date();
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("CET"));
cal.setTime(today);
cal.add(Calendar.MONTH, -3);
Date before = cal.getTime();
System.out.println("Today is : "+ new SimpleDateFormat(dateFormat).format(today));
System.out.println("-3 months: "+ new SimpleDateFormat(dateFormat).format(before));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment