Skip to content

Instantly share code, notes, and snippets.

@wezoalves
Last active December 14, 2015 08:49
Show Gist options
  • Save wezoalves/5060300 to your computer and use it in GitHub Desktop.
Save wezoalves/5060300 to your computer and use it in GitHub Desktop.
JAVA - Calendar
// get times
Calendar now = Calendar.getInstance();
int year = now.get(Calendar.YEAR);
int month = now.get(Calendar.MONTH); // Note: zero based!
int day = now.get(Calendar.DAY_OF_MONTH);
int hour = now.get(Calendar.HOUR_OF_DAY);
int minute = now.get(Calendar.MINUTE);
int second = now.get(Calendar.SECOND);
int millis = now.get(Calendar.MILLISECOND);
// end get times
// get start and end date of a year
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, 2012);
cal.set(Calendar.WEEK_OF_YEAR, 1);
cal.set(Calendar.DAY_OF_WEEK, 1);
Date start = cal.getTime();
cal.set(Calendar.YEAR, 2012);
cal.set(Calendar.MONTH, 11);
cal.set(Calendar.DAY_OF_MONTH, 31);
Date end = cal.getTime();
// end get start and end date of a year
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment