Skip to content

Instantly share code, notes, and snippets.

@alexchiri
Last active December 22, 2015 14:18
Show Gist options
  • Save alexchiri/6484337 to your computer and use it in GitHub Desktop.
Save alexchiri/6484337 to your computer and use it in GitHub Desktop.
Playing around with the add method of the Calendar.
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