Today in this tutorial we will look how to work with date and times in Java programming.
Date and time is very important for every application and managing that date and time in your specified format, specified time zone and readable format as you want to get fetch from java program.
We are going to use some methods of LocalDate, LocalTime and LocalDateTime classes.
now() : This method return the current system date and time. If you call this now() using the LocalDate then it will give you date only not time stamp. But the same now if you call using the object of LocalTime then that will return time stamp only not date.
minusMonths() : This method will reduce number of months from your given date. e.g. if your date is 2017-06-06 and when you will use minusMonths(2) then your date will become 2017-04-06.
plusMonths() : This method will add number of more months in your given date. e.g. if your date is 2017-04-06 and when you will use plusMonths(2) then your date will become 2017-06-06.
getDayOfWeek(): This method will return name of the day on a given time.
isBefore(): this getBefore method will return true or false by comparing the two given dates. e.g birthDate.isBefore(date.now()); here this will compare your birthdate is before current date or not. in the same way you can use the isAfter() as well.
Below is the complete example (ready to use) which you can just copy and paste in your eclipse and execute to see how different Date methods are working in Java.
package abctest;
import java.time.LocalDateTime;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import static java.time.temporal.TemporalAdjusters.*; // this import we have used to use next()import static java.time.DayOfWeek.*;
public class DateEx {
//here we have created three objects of LocalDate class. LocalDate now, bDate, today;
public static void main(String[] args) {
DateEx dateex=new DateEx();
// here we have created object local date(ld) from LocalDateTime.
LocalDateTime ld=LocalDateTime.now();
System.out.println(ld);
String locaDate=ld.format(DateTimeFormatter.ISO_DATE);
System.out.println(locaDate);
dateex.setNow(LocalDate.now());
dateex.dateMethods();
}
public void dateMethods(){
System.out.println("Date Methods:");
System.out.println("Date: " +getNow());
//below line is to fetch the local time from your system.
System.out.println("Time: " +LocalTime.now());
//minusMonths() is to minus the number of months from a given date.
bDate=getNow().minusMonths(12);
//isBefore() method is used to compare the earlier date from two given dates.
System.out.println("Birthaday: "+bDate +" Is dbirthday before current date? "+bDate.isBefore(now));
//To get the name of the for a date.
System.out.println("He born on day: " +bDate.getDayOfWeek());
//On what date next Monday will be from a given date.
System.out.println("Next Monday will be on " +getNow().with(next(MONDAY)));
//From which era your date belongs from either BCE or AD.
System.out.println("Era: "+bDate.getEra());
}
public LocalDate getNow() {
return now;
}
public void setNow(LocalDate now) {
this.now = now;
}
}
www.ukdancefloorhire.co.uk
ReplyDeletehire a dance floor
stage hire
led furniture hire
led dance floor hire
led dance floor hire
Buy gift online
ReplyDeletePersonalised Gifts
Personalised Wedding Gift
Personalised Birthday Gifts
Personalised Gift Ideas
Wedding Gift Ideas