Found 4337 Articles for Java 8

How to count days between two dates in Java

Nancy Den
Updated on 30-Jul-2019 22:30:25

1K+ Views

Let us first set two dates:LocalDate date1 = LocalDate.of(2019, 4, 16); LocalDate date2 = date1.with(Month.MAY).withDayOfMonth(04);Now, count the dates between both the above dates using between():int numDays = Period.between(date1, date2).getDays();Exampleimport java.time.LocalDate; import java.time.Month; import java.time.Period; public class Demo {    public static void main(String[] argv) {       LocalDate date1 = LocalDate.of(2019, 4, 16);       LocalDate date2 = date1.with(Month.MAY).withDayOfMonth(04);       int numDays = Period.between(date1, date2).getDays();       System.out.println("Number of days between two dates = "+numDays);    } }OutputNumber of days between two dates = 18

How to check whether the given date represents weekend in Java

Nancy Den
Updated on 30-Jul-2019 22:30:25

1K+ Views

At first, display the current date:LocalDate date = LocalDate.now();Now, get the day of week from the above Date (current date):DayOfWeek day = DayOfWeek.of(date.get(ChronoField.DAY_OF_WEEK));On the basis of the above result, use SWITCH to check for the current day. If the day is SATURDAY/SUNDAY, then it’s Weekend.Exampleimport java.time.DayOfWeek; import java.time.temporal.ChronoField; import java.time.LocalDate; public class Demo {    public static void main(String[] argv) {       LocalDate date = LocalDate.now();       DayOfWeek day = DayOfWeek.of(date.get(ChronoField.DAY_OF_WEEK));       switch (day) {          case SATURDAY:             System.out.println("Weekend - Saturday");         ... Read More

How to get days, months and years between two Java LocalDate?

Nancy Den
Updated on 30-Jul-2019 22:30:25

849 Views

Set the two Java dates:LocalDate date1 = LocalDate.of(2019, 3, 25); LocalDate date2 = LocalDate.of(2019, 4, 29);Now, get the difference between two dates with Period class between() method:Period p = Period.between(date1, date2);Now, get the years, month and days:p.getYears() p.getMonths() p.getDays()Exampleimport java.time.LocalDate; import java.time.Period; public class Demo {    public static void main(String[] args) {       LocalDate date1 = LocalDate.of(2019, 3, 25);       LocalDate date2 = LocalDate.of(2019, 4, 29);       System.out.println("Date 1 = "+date1);       System.out.println("Date 2 = "+date2);       Period p = Period.between(date1, date2);       System.out.println("Period = "+p);   ... Read More

Java Program to add Period to LocalDate

Nancy Den
Updated on 30-Jul-2019 22:30:25

385 Views

Let us first set a Period with month and days:Period p = Period.ofMonths(5).plusDays(15);Now set the LocalDate:LocalDate date = LocalDate.of(2019, 4, 10);Add period to LocalDate:LocalDate res = date.plus(p);Exampleimport java.time.LocalDate; import java.time.Period; public class Demo {    public static void main(String[] args) {       Period p = Period.ofMonths(5).plusDays(15);       LocalDate date = LocalDate.of(2019, 4, 10);       System.out.println("Date = "+date);       LocalDate res = date.plus(p);       System.out.println("Updated date = "+res);    } }OutputDate = 2019-04-10 Updated date = 2019-09-25

Java Program to minus seconds and nanoseconds from Instant

Daniol Thomas
Updated on 30-Jul-2019 22:30:25

201 Views

Let us first set an Instant:Instant now = Instant.ofEpochMilli(184142540078l);Let us now minus seconds from Instant:Instant resSeconds = now.minusSeconds(50);Let us now minus nanoseconds from Instant:Instant resNanoSeconds = now.minusNanos(10000);Exampleimport java.time.Instant; public class Demo {    public static void main(String[] args) {       Instant now = Instant.ofEpochMilli(184142540078l);       System.out.println(now);       Instant resSeconds = now.minusSeconds(50);       System.out.println("After subtracting seconds = "+resSeconds);       Instant resNanoSeconds = now.minusNanos(10000);       System.out.println("After subtracting nanoseconds = "+resNanoSeconds);    } }Output1975-11-02T06:42:20.078Z After subtracting seconds = 1975-11-02T06:41:30.078Z After subtracting nanoseconds = 1975-11-02T06:42:20.077990Z

How to get the seconds and minutes between two Instant timestamps in Java

Krantik Chavan
Updated on 30-Jul-2019 22:30:25

2K+ Views

The following are the two Instant timestamps:Instant one = Instant.ofEpochSecond(1355836728); Instant two = Instant.ofEpochSecond(1355866935);Get the Duration between both the Instant:Duration res = Duration.between(one, two);Now, get the seconds between the two timestamps:long seconds = res.getSeconds();Now, get the minutes between the two timestamps:long minutes = res.abs().toMinutes();Exampleimport java.time.Duration; import java.time.Instant; public class Demo {    public static void main(String[] args) {       Instant one = Instant.ofEpochSecond(1355836728);       Instant two = Instant.ofEpochSecond(1355866935);       Duration res = Duration.between(one, two);       System.out.println(res);       long seconds = res.getSeconds();       System.out.println("Seconds between Durations = "+seconds);   ... Read More

How to get the duration between two Instant timestamps in Java

Krantik Chavan
Updated on 30-Jul-2019 22:30:25

18K+ Views

Let us first set two Instants with ofEpochSeconds() method using seconds from the epoch of 1970-01- 01T00:00:00Z.Now get the duration between the above two Instant:Duration res = Duration.between(one, two);Exampleimport java.time.Duration; import java.time.Instant; public class Demo {    public static void main(String[] args) {       Instant one = Instant.ofEpochSecond(1845836728);       Instant two = Instant.ofEpochSecond(1845866935);       Duration res = Duration.between(one, two);       System.out.println(res);    } }OutputPT8H23M27S

Java Program to get Milli seconds from Instant

Krantik Chavan
Updated on 30-Jul-2019 22:30:25

177 Views

Create an Instant and get an Instant using milliseconds passed as parameter from the epoch of 1970-01- 01T00:00:00Z. This is done using ofEpochMilli():Instant instant = Instant.ofEpochMilli(342627282920l);Now, get the Epoch milliseconds from Instant:instant. toEpochMilli();Exampleimport java.time.Instant; public class Demo {    public static void main(String[] args) {       Instant instant = Instant.ofEpochMilli(1262347200000l);       long res = instant.toEpochMilli();       System.out.println(res);    } }Output1262347200000

Java Program to get Epoch seconds from Instant

Krantik Chavan
Updated on 30-Jul-2019 22:30:25

172 Views

Create an Instant and get an Instant using milliseconds passed as parameter from the epoch of 1970-01- 01T00:00:00Z. This is done using ofEpochMilli():Instant instant = Instant.ofEpochMilli(342627282920l);Now, get the Epoch seconds from Instant:instant.getEpochSecond();Exampleimport java.time.Instant; public class Demo {    public static void main(String[] args) {       Instant instant = Instant.ofEpochMilli(342627282920l);       long res = instant.getEpochSecond();       System.out.println(res);    } }Output342627282

Java Program to create Instant from Epoch second and millisecond

Krantik Chavan
Updated on 30-Jul-2019 22:30:25

993 Views

Create Instant from Epoch SecondExampleimport java.time.Instant; public class Demo {    public static void main(String[] args) {       Instant instant = Instant.ofEpochSecond(282829279);       System.out.println(instant);    } }Output1978-12-18T11:41:19ZCreate Instant from Epoch MillisecondsExampleimport java.time.Instant; public class Demo {    public static void main(String[] args) {       Instant instant = Instant.ofEpochMilli(272827282728l);       System.out.println(instant);    } }Output1978-08-24T17:21:22.728Z

Advertisements