Object Oriented Programming Articles

Page 573 of 589

How to format and display date in Java as '201904'

Krantik Chavan
Krantik Chavan
Updated on 30-Jul-2019 149 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

Read More

How can I display Java date as '12/04/2019'

Krantik Chavan
Krantik Chavan
Updated on 30-Jul-2019 134 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

Read More

Java Program to convert a list to a read-only list

Krantik Chavan
Krantik Chavan
Updated on 30-Jul-2019 266 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

Java Program to check for the supported attribute via java.nio.file.FileStore

Daniol Thomas
Daniol Thomas
Updated on 30-Jul-2019 154 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

Java Program to format date as Apr 19, 2019, 1:27 PM

Daniol Thomas
Daniol Thomas
Updated on 30-Jul-2019 230 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

Read More

Java Program to format LocalDateTime as ISO_WEEK_DATE format

Daniol Thomas
Daniol Thomas
Updated on 30-Jul-2019 226 Views

At first, set the date:LocalDateTime dateTime = LocalDateTime.of(2019, Month.SEPTEMBER, 9, 10, 20);Now, format the datetime as ISO_WEEK_DATE format:String str = dateTime.format(DateTimeFormatter.ISO_WEEK_DATE);Exampleimport java.time.LocalDateTime; import java.time.Month; import java.time.format.DateTimeFormatter; public class Demo {    public static void main(String[] args) {       LocalDateTime dateTime = LocalDateTime.of(2019, Month.SEPTEMBER, 9, 10, 20);       System.out.println("DateTime = "+dateTime);       String str = dateTime.format(DateTimeFormatter.ISO_WEEK_DATE);       System.out.println("Formatted date = "+str);    } }OutputDateTime = 2019-09-09T10:20 Formatted date = 2019-W37-1

Read More

Java Program to format LocalTimeDate as BASIC_ISO_DATE format

Nancy Den
Nancy Den
Updated on 30-Jul-2019 566 Views

At first, set the date:LocalDateTime dateTime = LocalDateTime.of(2019, Month.SEPTEMBER, 6, 20, 10);Now, format the datetime as BASIC_ISO_DATE format:String str = dateTime.format(DateTimeFormatter.BASIC_ISO_DATE);Exampleimport java.time.LocalDateTime; import java.time.Month; import java.time.format.DateTimeFormatter; public class Demo {    public static void main(String[] args) {       LocalDateTime dateTime = LocalDateTime.of(2019, Month.SEPTEMBER, 6, 20, 10);       System.out.println("DateTime = "+dateTime);       String str = dateTime.format(DateTimeFormatter.BASIC_ISO_DATE);       System.out.println("Formatted date = "+str);    } }OutputDateTime = 2019-09-06T20:10 Formatted date = 20190906

Read More

How to sort an array with customized Comparator in Java?

Krantik Chavan
Krantik Chavan
Updated on 30-Jul-2019 3K+ 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

Java Program to get Temporal Queries precision

karthikeya Boyini
karthikeya Boyini
Updated on 30-Jul-2019 189 Views

To get Temporal Queries precision, use the TemporalQuery interface with the precision() method of the TemporalQueries −TemporalQueryprecision = TemporalQueries.precision(); Get the precision for LocalDate: LocalDate.now().query(precision) Get the precision for LocalTime: LocalTime.now().query(precision) Get the precision for YearMonth: YearMonth.now().query(precision) Get the precision for Year: Year.now().query(precision)Exampleimport java.time.LocalDate; import java.time.LocalDateTime; import java.time.LocalTime; import java.time.Year; import java.time.YearMonth; import java.time.temporal.TemporalQueries; import java.time.temporal.TemporalQuery; import java.time.temporal.TemporalUnit; public class Demo {    public static void main(String[] args) {       TemporalQueryprecision = TemporalQueries.precision();       System.out.println("TemporalQueries precision...");       System.out.println(LocalDate.now().query(precision));       System.out.println(LocalTime.now().query(precision));       System.out.println(LocalDateTime.now().query(precision));       System.out.println(YearMonth.now().query(precision));       System.out.println(Year.now().query(precision)); ...

Read More

How do I discover the Quarter of a given Date in Java?

Samual Sam
Samual Sam
Updated on 30-Jul-2019 2K+ Views

Let us first get the current date −LocalDate currentDate = LocalDate.now();Now, use the Calendar class and set the locale −Calendar cal = Calendar.getInstance(Locale.US);Now, get the month −int month = cal.get(Calendar.MONTH);Find the Quarter −int quarter = (month / 3) + 1;Exampleimport java.time.LocalDate; import java.util.Calendar; import java.util.Locale; public class Demo {    public static void main(String[] args) {       LocalDate currentDate = LocalDate.now();       System.out.println("Current Date = "+currentDate);       Calendar cal = Calendar.getInstance(Locale.US);       int month = cal.get(Calendar.MONTH);       int quarter = (month / 3) + 1;       System.out.println("Quarter = "+quarter);    } }OutputCurrent Date = 2019-04-12 Quarter = 2

Read More
Showing 5721–5730 of 5,881 articles
« Prev 1 571 572 573 574 575 589 Next »
Advertisements