Created
October 20, 2022 05:33
-
-
Save rmg007/a2f44268e33a92e981395dab52022dc9 to your computer and use it in GitHub Desktop.
Date Calendar Examples Lecture
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.text.ParseException; | |
| import java.text.SimpleDateFormat; | |
| import java.util.Calendar; | |
| import java.util.Date; | |
| import java.util.GregorianCalendar; | |
| /* | |
| Date starts from 1900 | |
| Unix epoch: Time starts from 1970-1-1 | |
| */ | |
| public class DateCalendarExamples{ | |
| public static void main(String[] args) throws ParseException { | |
| SimpleDateFormat simpleDateFormat = | |
| new SimpleDateFormat("dd/MM/yyyy"); | |
| Date today = new Date(); | |
| System.out.println(simpleDateFormat.format(today)); | |
| System.out.println(today.getDate()); | |
| System.out.println(today.getYear()); | |
| System.out.println(today.getDay()); | |
| System.out.println(today.getMonth()); | |
| System.out.println(today.getTime()); | |
| Date date = simpleDateFormat.parse("29/12/1979"); | |
| System.out.println(date); | |
| //------------------------------------------- | |
| System.out.println(new Date()); | |
| Calendar calendar = new GregorianCalendar(); | |
| System.out.println(calendar.get(Calendar.ERA)); | |
| System.out.println(calendar.get(Calendar.YEAR)); | |
| System.out.println(calendar.get(Calendar.MONTH)); // index | |
| System.out.println(calendar.get(Calendar.WEEK_OF_YEAR)); | |
| System.out.println(calendar.get(Calendar.DATE)); | |
| System.out.println(calendar.get(Calendar.DAY_OF_MONTH)); | |
| System.out.println(calendar.get(Calendar.DAY_OF_YEAR)); | |
| System.out.println(calendar.get(Calendar.DAY_OF_WEEK)); // not index | |
| System.out.println(calendar.get(Calendar.HOUR)); | |
| System.out.println(calendar.get(Calendar.HOUR_OF_DAY)); | |
| System.out.println(calendar.get(Calendar.MINUTE)); | |
| System.out.println(calendar.get(Calendar.SECOND)); | |
| System.out.println(calendar.get(Calendar.MILLISECOND)); | |
| System.out.println(calendar.get(Calendar.AM_PM)); // 0: AM, 1: PM | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment