Get the Current Date:
Date today = new Date();
System.out.println(“Current date: “ + today);
OUTPUT:
date: Sat May 04 02:29:59 IST 2019
Difference between dates:
Check Whether Time Interval has Passed:
Date deadLineDate = new Date(deadLine); Date currentTime = new Date();
while(!currentTime.after(deadLineDate))// Check whether currentTime is after deadLineDate {
Using getTime()
public class Main { public static void main(String[] args) {
Date startTime = new Date();
Date deadLineDate = new Date(deadLine); Date currentTime = new Date();
while(currentTime.getTime() < deadLineDate.getTime())// Check whether currentTime is after deadLineDate {
}}
before()
equals()
also work with same syntax as after()
Various get and set methods:
get methods
Date currentTime = new Date();int hours = currentTime.getHours(); // hours passed since midnight.
int mins = currentTime.getMinutes(); // hours passed since midnight.int secs = currentTime.getSeconds(); // seconds passed since midnight.
set methods
Date yearStartTime = new Date();
yearStartTime.setHours(0);
yearStartTime.setMinutes(0);
yearStartTime.setSeconds(0);
yearStartTime.setDate(1); // First day of month
yearStartTime.setMonth(0); // January (months are indexed from 0 to 11)
Calendar class:
Calendar calendar = new GregorianCalendar(2017, 0 , 25);
GregorianCalendar calendar = new GregorianCalendar(2017, Calendar.JANUARY , 25);
set the time 10:30:15 1 Jan 2019
public static void main(String[] args) { Calendar calendar = new GregorianCalendar(); calendar.set(Calendar.YEAR, 2019); calendar.set(Calendar.MONTH, 0); calendar.set(Calendar.DAY_OF_MONTH, 1); calendar.set(Calendar.HOUR_OF_DAY, 10); calendar.set(Calendar.MINUTE, 30); calendar.set(Calendar.SECOND, 15); System.out.println(calendar.getTime());}
Output:
Tue Jan 01 10:30:15 IST 2019
—————————————————————-
set the time 10:30:15 1 Jan 2019
Output:
Tue Jan 01 10:30:15 IST 2019
—————————————————————-
public static void main(String[] args) { Calendar calendar = new GregorianCalendar(2019, Calendar.JANUARY , 1); calendar.set(Calendar.HOUR, 10); calendar.set(Calendar.MINUTE, 30); calendar.set(Calendar.SECOND, 15); calendar.add(Calendar.MONTH, –2); // Date will automatically adjust into 2018. System.out.println(calendar.getTime());}
output:
Thu Nov 01 10:30:15 IST 2018
——————————————————————
roll() : This method will make change only in particular part, In this example
only Month will be reduced to Nov but years and other time will not be changed.
public static void main(String[] args) { Calendar calendar = new GregorianCalendar(2019, Calendar.JANUARY , 1); calendar.set(Calendar.HOUR, 10); calendar.set(Calendar.MINUTE, 30); calendar.set(Calendar.SECOND, 15); calendar.roll(Calendar.MONTH, –2); // Date will automatically adjust into 2018. System.out.println(calendar.getTime());}
Output:
Fri Nov 01 10:30:15 IST 2019
——————————————————————
output:
Thu Nov 01 10:30:15 IST 2018
——————————————————————
roll() : This method will make change only in particular part, In this example
only Month will be reduced to Nov but years and other time will not be changed.
public static void main(String[] args) { Calendar calendar = new GregorianCalendar(2019, Calendar.JANUARY , 1); calendar.set(Calendar.HOUR, 10); calendar.set(Calendar.MINUTE, 30); calendar.set(Calendar.SECOND, 15); calendar.roll(Calendar.MONTH, –2); // Date will automatically adjust into 2018. System.out.println(calendar.getTime());}
Output:
Fri Nov 01 10:30:15 IST 2019
——————————————————————
get and set:
calendar.set(Calendar.HOUR, 10);calendar.set(Calendar.MINUTE, 30);calendar.set(Calendar.SECOND, 15); System.out.println(“Year: “ + calendar.get(Calendar.YEAR));System.out.println(“Month: “ + calendar.get(Calendar.MONTH));
——————————————————————-
——————————————————————-
date format:
SimpleDateFormat dateFormat = new SimpleDateFormat(“EEEE, MMMM d, yyyy”);
Calendar calendar = new GregorianCalendar(2019, Calendar.JANUARY , 1);
System.out.println(dateFormat.format(calendar.getTime()));
output:
Tuesday, January 1, 2019
Calendar calendar = new GregorianCalendar(2019, Calendar.JANUARY , 1);
System.out.println(dateFormat.format(calendar.getTime()));
output:
Tuesday, January 1, 2019



