Found 33676 Articles for Programming

Java Program to create LocalDateTime from Clock

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

481 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

Java Program to format LocalTimeDate as BASIC_ISO_DATE format

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

502 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

Java Program to format LocalDateTime as ISO_WEEK_DATE format

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

178 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

How to format Java LocalDateTime as ISO_DATE_TIME format

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

4K+ Views

At first, set the date:LocalDateTime dateTime = LocalDateTime.of(2019, Month.JULY, 9, 10, 20);Now, format the datetime as ISO_DATE_TIME format:String str = dateTime.format(DateTimeFormatter.ISO_DATE_TIME);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.JULY, 9, 10, 20);       System.out.println("DateTime = "+dateTime);       String str = dateTime.format(DateTimeFormatter.ISO_DATE_TIME);       System.out.println("Formatted date = "+str);    } }OutputDateTime = 2019-07-09T10:20 Formatted date = 2019-07-09T10:20:00

Java Program to format date as Apr 14 2019 01:35 PM IST

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

117 Views

To format and display datetime, you need to use DateTimeFormatter and use the pattern as:DateTimeFormatter dtFormat = DateTimeFormatter.ofPattern("MMM dd yyyy hh:mm a z");Above, the z is the timezone:MMM dd yyyy hh:mm a zNow, use the following for zone:ZonedDateTime dateTime = ZonedDateTime.now();Exampleimport java.time.ZonedDateTime; import java.time.format.DateTimeFormatter; public class Demo {    public static void main(String[] argv) {       DateTimeFormatter dtFormat = DateTimeFormatter.ofPattern("MMM dd yyyy hh:mm a z");       ZonedDateTime dateTime = ZonedDateTime.now();       String res = dateTime.format(dtFormat);       System.out.printf("Date = %s %n", res);    } }OutputDate = Apr 14 2019 01:35 PM IST

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

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

191 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

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

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

116 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 convert a list to a read-only list

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

219 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

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

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

87 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

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

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

111 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

Advertisements