Last active
December 22, 2015 14:18
-
-
Save alexchiri/6484337 to your computer and use it in GitHub Desktop.
Playing around with the add method of the Calendar.
This file contains hidden or 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.util.Calendar; | |
public class TimeTravelling { | |
public static void main(String[] args) { | |
Calendar now = Calendar.getInstance(); | |
System.out.println("Greetings from the present: " + now.getTime()); | |
Calendar twoMonthsAndTeoWeeksAgo = Calendar.getInstance(); | |
twoMonthsAndTeoWeeksAgo.setTime(now.getTime()); | |
twoMonthsAndTeoWeeksAgo.add(Calendar.MONTH, -2); | |
twoMonthsAndTeoWeeksAgo.add(Calendar.DATE, -14); | |
System.out.println("Greetings from the past: " + twoMonthsAndTeoWeeksAgo.getTime()); | |
Calendar backInThePresent1 = Calendar.getInstance(); | |
backInThePresent1.setTime(twoMonthsAndTeoWeeksAgo.getTime()); | |
backInThePresent1.add(Calendar.MONTH, 2); | |
backInThePresent1.add(Calendar.DATE, 14); | |
System.out.println("Attempt to get back in the present no 1: " + backInThePresent1.getTime()); | |
Calendar backInThePresent2 = Calendar.getInstance(); | |
backInThePresent2.setTime(twoMonthsAndTeoWeeksAgo.getTime()); | |
backInThePresent2.add(Calendar.DATE, 14); | |
backInThePresent2.add(Calendar.MONTH, 2); | |
System.out.println("Attempt to get back in the present no 1: " + backInThePresent2.getTime()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment