
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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 7442 Articles for Java

192 Views
To format and display datetime, you need to use DateTimeFormatter. The format style is MEDIUM and SHORT:DateTimeFormatter formatter = DateTimeFormatter .ofLocalizedDateTime(FormatStyle.MEDIUM, FormatStyle.SHORT);Display the formatted date:formatter.format(LocalDateTime.now()Exampleimport java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.time.format.FormatStyle; public class Demo { public static void main(String[] args) { DateTimeFormatter formatter = DateTimeFormatter .ofLocalizedDateTime(FormatStyle.MEDIUM, FormatStyle.SHORT); System.out.println("Formatted Date = "+formatter.format(LocalDateTime.now())); } }OutputFormatted Date = Apr 19, 2019, 1:27 PM

119 Views
Following is our file:Path p = Paths.get("E:/input.txt"); FileStore file = Files.getFileStore(p);Now, check for the supported attributes one by one:FileAttributeView = file.supportsFileAttributeView(FileAttributeView.class) PosixFileAttributeView = file.supportsFileAttributeView(PosixFileAttributeView.class) BasicFileAttributeView = file.supportsFileAttributeView(BasicFileAttributeView.class)Exampleimport java.nio.file.FileStore; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.attribute.BasicFileAttributeView; import java.nio.file.attribute.FileAttributeView; import java.nio.file.attribute.FileOwnerAttributeView; import java.nio.file.attribute.PosixFileAttributeView; public class Demo { public static void main(String[] args) throws Exception { Path p = Paths.get("E:/input.txt"); FileStore file = Files.getFileStore(p); System.out.println("FileAttributeView = " + file.supportsFileAttributeView(FileAttributeView.class)); System.out.println("PosixFileAttributeView = "+ file.supportsFileAttributeView(PosixFileAttributeView.class)); System.out.println("BasicFileAttributeView = "+ file.supportsFileAttributeView(BasicFileAttributeView.class)); System.out.println("FileOwnerAttributeView supported = "+ file.supportsFileAttributeView(FileOwnerAttributeView.class)); ... Read More

221 Views
Let’s say the following is our list which isn’t read-only:List < Integer > list = new ArrayList < Integer > (); list.add(10); list.add(20); list.add(30); list.add(40); list.add(50); list.add(20); list.add(40); list.add(50);Convert the above list to Read-only:list = Collections.unmodifiableList(list);On conversion, now you won’t be add or remove elements from the List. Let us see an example:The following program will give an error because we first update the list to read only and then try to remove an element from it, which is not possible now. The reason is we have converted the list to readonly and you cannot add or remove element from ... Read More

89 Views
To format and display date like this i.e. Day/Month/Year, you need to set the pattern:dd/MM/yyyyAt first, set a LocalDate:LocalDate localDate = LocalDate.now();Now format and display date as '12/04/2019':localDate.format(DateTimeFormatter.ofPattern("dd/MM/yyyy"))Exampleimport java.time.LocalDate; import java.time.format.DateTimeFormatter; public class Demo { public static void main(String[] args) { LocalDate date = LocalDate.now(); System.out.println("Date = "+date); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy"); System.out.println("Formatted Date = "+date.format(formatter)); } }OutputDate = 2019-04-12 Formatted Date = 12/04/2019

113 Views
To format and display date like this i.e. YearMonth, you need to set the pattern:uuuuMMAt first, set a LocalDate:LocalDate localDate = LocalDate.now();Now format and display date as ‘201904’:localDate.format(DateTimeFormatter.ofPattern("uuuuMM"))Exampleimport java.time.LocalDate; import java.time.format.DateTimeFormatter; public class Demo { public static void main(String[] args) { LocalDate localDate = LocalDate.now(); System.out.println("Date = "+localDate); System.out.println("Date (Year and Month) = "+localDate.format(DateTimeFormatter.ofPattern("uuuuMM"))); } }OutputDate = 2019-04-19 Date (Year and Month) = 201904

814 Views
In this article, we will learn to convert milliseconds to readable strings in Java. Converting milliseconds into a human-readable format such as hours, minutes, seconds, and milliseconds is a common programming task, especially in time-sensitive applications. Problem Statement The challenge is to convert a given time in milliseconds into a readable string that shows hours, minutes, seconds, and remaining milliseconds. Input long millis = 5000000; Output Hours = 1 Minutes = 23 Seconds = 20Milliseconds = 0 1 hr(s) 23 min(s) 20 sec(s) 0 ms Approaches to convert millisecond to readable string Following are the two different approaches to converting milliseconds ... Read More

4K+ Views
At first set a LocalDateTime:LocalDate date = LocalDate.now(); LocalTime time = LocalTime.now(); LocalDateTime dateTime = LocalDateTime.of(date, time);Now, convert the LocalDateTime to LocalDate and LocalTime:LocalDate localDate = LocalDateTime.now().toLocalDate(); LocalTime localTime = LocalDateTime.now().toLocalTime();Exampleimport java.time.LocalDate; import java.time.LocalDateTime; import java.time.LocalTime; public class Demo { public static void main(String[] args) { LocalDate date = LocalDate.now(); LocalTime time = LocalTime.now(); LocalDateTime dateTime = LocalDateTime.of(date, time); System.out.println("DateTime = "+dateTime); LocalDate localDate = LocalDateTime.now().toLocalDate(); LocalTime localTime = LocalDateTime.now().toLocalTime(); System.out.println("Date = "+localDate); System.out.println("Time = ... Read More

96 Views
Let us first create a TreeSet and add elements to it:TreeSet treeSet = new TreeSet(); treeSet.add(20); treeSet.add(50); treeSet.add(100); treeSet.add(120); treeSet.add(150); treeSet.add(200); treeSet.add(250); treeSet.add(300); treeSet.add(350); treeSet.add(400);Now, let us get Tail Set from the TreeSet. In the below case, we will get elements above 200:SortedSet set = treeSet.tailSet(200);Exampleimport java.util.SortedSet; import java.util.TreeSet; public class Demo { public static void main(String[] args) { TreeSet treeSet = new TreeSet(); treeSet.add(20); treeSet.add(50); treeSet.add(100); treeSet.add(120); treeSet.add(150); treeSet.add(200); treeSet.add(250); treeSet.add(300); ... Read More

115 Views
Let us first create a TreeSet and add elements:TreeSet treeSet = new TreeSet(); treeSet.add(10); treeSet.add(20); treeSet.add(30); treeSet.add(40); treeSet.add(50); treeSet.add(60); treeSet.add(70); treeSet.add(80); treeSet.add(90); treeSet.add(100);Now, let’s say you need to set sub set from 50 to 70, then use the subset() for it:SortedSet sub = treeSet.subSet(50, 70); System.out.println("Sub Set = " + sub);Exampleimport java.util.TreeSet; import java.util.SortedSet; public class Demo { public static void main(String[] args) { TreeSet treeSet = new TreeSet(); treeSet.add(10); treeSet.add(20); treeSet.add(30); treeSet.add(40); treeSet.add(50); treeSet.add(60); ... Read More

3K+ Views
Let us first create an int array −int[] arr = new int[10];Now, fill array values. Here, the numbers get added from the index 2 to 7 −Arrays.fill(arr, 2, 7, 100);Example Live Demoimport java.util.Arrays; public class Demo { public static void main(String[] args) { int[] arr = new int[10]; System.out.println("Array = "+Arrays.toString(arr)); Arrays.fill(arr, 2, 7, 100); System.out.println("Fill = "+Arrays.toString(arr)); } }OutputArray = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] Fill = [0, 0, 100, 100, 100, 100, 100, 0, 0, 0]