- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 4337 Articles for Java 8
404 Views
To get the seconds since the beginning of epochs, you need to use Instant. The method here used is ofEpochSecond() method. The epoch is the number of seconds that have elapsed since 00::00:00 Thursday, 1 January 1970.Get the seconds with ChronoUnit.SECONDS −long seconds = Instant.ofEpochSecond(0L).until(Instant.now(), ChronoUnit.SECONDS);Exampleimport java.time.Instant; import java.time.temporal.ChronoUnit; public class Demo { public static void main(String[] args) { long seconds = Instant.ofEpochSecond(0L).until(Instant.now(), ChronoUnit.SECONDS); System.out.println("Seconds since the beginning of the Java epoch = "+seconds); } }OutputSeconds since the beginning of the Java epoch = 1555053202
1K+ Views
To get day of week as string, first let us display the current date −LocalDate currentDate = LocalDate.now();Now, use DayOfWeek to get the day of week from the current date −DayOfWeek day = currentDate.getDayOfWeek();Get the day of week as string −String weekName = day.name();Exampleimport java.time.DayOfWeek; import java.time.LocalDate; public class Demo { public static void main(String[] args) { LocalDate currentDate = LocalDate.now(); System.out.println("Current Date = "+currentDate); DayOfWeek day = currentDate.getDayOfWeek(); int weekVal = day.getValue(); String weekName = day.name(); System.out.println("Week Number = "+weekVal); ... Read More
144 Views
Let us get the timezone id strings for timezone America. For this, use the get.AvailableZoneIds() method −ZoneId.getAvailableZoneIds().stream().filter(s ->s.startsWith("America")) .forEach(System.out::println);With that, we have used the forEach to display all the timezones in America −America/Cuiaba America/Marigot America/El_Salvador America/Guatemala America/Belize America/Panama America/Managua America/Indiana/Petersburg America/Chicago America/Tegucigalpa America/Eirunepe America/Miquelon . . .Exampleimport java.time.ZoneId; public class Demo { public static void main(String[] args) { System.out.println("TimeZones in the US"); ZoneId.getAvailableZoneIds().stream().filter(s ->s.startsWith("America")) .forEach(System.out::println); } }OutputTimeZones in the US America/Cuiaba America/Marigot America/El_Salvador America/Guatemala America/Belize America/Panama America/Managua America/Indiana/Petersburg America/Chicago America/Tegucigalpa America/Eirunepe America/Miquelon America/Argentina/Catamarca America/Grand_Turk America/Argentina/Cordoba America/Araguaina America/Argentina/Salta America/Montevideo America/Manaus ... Read More
717 Views
Let’s say the following are the two dates for which we want the milliseconds difference −LocalDateTime dateOne = LocalDateTime.now(); LocalDateTime dateTwo = LocalDateTime.of(2019, 4, 10, 11, 20);Now, get the milliseconds between the above two dates with MILLIS.between −long res = MILLIS.between(dateOne, dateTwo);Exampleimport java.time.LocalDateTime; import static java.time.temporal.ChronoUnit.MILLIS; public class Demo { public static void main(String[] argv) { LocalDateTime dateOne = LocalDateTime.now(); LocalDateTime dateTwo = LocalDateTime.of(2019, 4, 10, 11, 20); System.out.printf("Date One = "+dateOne); System.out.printf("Date Two = "+dateTwo); long res = MILLIS.between(dateOne, dateTwo); ... Read More
219 Views
Let us first create signed byte −byte signedVal = -100;Now convert a byte to an unsigned integer −int unsignedVal = Byte.toUnsignedInt(signedVal);Examplepublic class Demo { public static void main(String[] args) { byte signedVal = -100; int unsignedVal = Byte.toUnsignedInt(signedVal); System.out.println("Signed value (byte) = " + signedVal); System.out.println("Unsigned value (byte) = " + unsignedVal); } }OutputSigned value (byte) = -100 Unsigned value (byte) = 156
617 Views
For distinct numbers, use Set, since all its implementations remove duplicates −Setset = new LinkedHashSet();Now, create a Random class object −Random randNum = new Random();Generate 10 distinct random numbers now with nextInt of the Random class −while (set.size() < 10) { set.add(randNum.nextInt(10)+1); }Exampleimport java.util.LinkedHashSet; import java.util.Random; import java.util.Set; public class Demo { public static void main(final String[] args) throws Exception { Random randNum = new Random(); Setset = new LinkedHashSet(); while (set.size() < 10) { set.add(randNum.nextInt(10)+1); } System.out.println("Distinct random numbers = "+set); } }OutputDistinct random numbers = [4, 6, 9, 1, 5, 2, 8, 7, 10, 3]
462 Views
Create an input stream and set the string:DataInputStream inputStream = new DataInputStream(new ByteArrayInputStream("pqrs tu v wxy z".getBytes()));The getBytes() method is used to convert a string into sequence of bytes and returns an array of bytes.Now return one single input byte:(char) inputStream.readByte()Exampleimport java.io.ByteArrayInputStream; import java.io.DataInputStream; public class Demo { public static void main(String[] args) throws Exception { DataInputStream inputStream = new DataInputStream(new ByteArrayInputStream("pqrs tu v wxy z".getBytes())); System.out.print((char) inputStream.readByte()); System.out.print((char) inputStream.readByte()); inputStream.close(); } }OutputPq
2K+ Views
Let’s say the following is our string array and we need to sort it:String[] str = { "Tom", "Jack", "Harry", "Zen", "Tim", "David" };Within the sort() method, create a customized comparator to sort the above string. Here, two strings are compared with each other and the process goes on:Arrays.sort(str, new Comparator < String > () { public int compare(String one, String two) { int val = two.length() - one.length(); if (val == 0) val = one.compareToIgnoreCase(two); return val; } });Exampleimport java.util.Arrays; import java.util.Comparator; public class Demo ... Read More
213 Views
Create two time instants:Instant time1 = Instant.now(); Instant time2 = Instant.now().plusSeconds(50);Use between() to get the duration between two time instants:long resMilli = Duration.between(time1, time2).toMillis();Exampleimport java.time.Duration; import java.time.Instant; public class Demo { public static void main(String[] args) { Instant time1 = Instant.now(); Instant time2 = Instant.now().plusSeconds(50); long resMilli = Duration.between(time1, time2).toMillis(); System.out.println("Duration between two time intervals = "+resMilli); } }OutputDuration between two time intervals = 50000
295 Views
At first, set the Clock:Clock clock = Clock.systemUTC();Now, create LocalDateTime:LocalDateTime dateTime = LocalDateTime.now(clock);Exampleimport java.time.Clock; import java.time.LocalDateTime; public class Demo { public static void main(String[] args) { Clock clock = Clock.systemUTC(); System.out.println("Clock = "+Clock.systemDefaultZone()); LocalDateTime dateTime = LocalDateTime.now(clock); System.out.println("LocalDateTime = "+dateTime); } }OutputClock = SystemClock[Asia/Calcutta] LocalDateTime = 2019-04-19T09:29:50.605820900