Created
March 25, 2014 20:33
-
-
Save aweijnitz/9770743 to your computer and use it in GitHub Desktop.
Illustration of basic date arithmetics in Java.
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
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