This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.time.Duration; | |
| import java.time.Instant; | |
| public class Main { | |
| public static void main(String[] args) { | |
| // get specific moment time in the beginning | |
| Instant start = Instant.now(); | |
| for (int i = 0; i < 1000; i++) { | |
| System.out.println("Hello, World!"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.time.LocalDateTime; | |
| import java.time.format.DateTimeFormatter; | |
| public class Main { | |
| public static void main(String[] args) { | |
| String str = "Welcome, today date & time: July 15, 2021 - 10:05 AM"; | |
| // get date & time part from String | |
| String dateStr = str.split("time:")[1].trim(); | |
| // Print String date & time | |
| System.out.println(dateStr); // July 15, 2021 - 10:05 AM |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.time.LocalDateTime; | |
| import java.time.format.DateTimeFormatter; | |
| public class Main { | |
| public static void main(String[] args) { | |
| LocalDateTime dateTime = LocalDateTime.of(1996, 03, 26, 19, 0); | |
| System.out.println(dateTime); // 1996-03-26T19:00 | |
| DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MMM dd, YYYY - hh:mm a"); | |
| String dateTimeStr = formatter.format(dateTime); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.time.LocalDate; | |
| import java.time.LocalDateTime; | |
| import java.time.LocalTime; | |
| public class Main { | |
| public static void main(String[] args) { | |
| // get current date & time | |
| LocalDateTime dateTime = LocalDateTime.now(); | |
| System.out.println(dateTime); // 2021-08-17T09:56:26.928961 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.time.LocalDateTime; | |
| public class Main { | |
| public static void main(String[] args) { | |
| // get current date & time | |
| LocalDateTime dateTime = LocalDateTime.now(); | |
| System.out.println(dateTime); // 2021-08-17T09:56:26.928961 | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.time.LocalTime; | |
| import java.time.format.DateTimeFormatter; | |
| public class Main { | |
| public static void main(String[] args) { | |
| LocalTime time = LocalTime.of(9, 00); | |
| // Formatting time | |
| DateTimeFormatter f = DateTimeFormatter.ofPattern("hh:mm a"); | |
| // 'a' will add time extension for 12 hours format |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.time.LocalTime; | |
| public class Main { | |
| public static void main(String[] args) { | |
| LocalTime time = LocalTime.now(); | |
| System.out.println(time); // 09:26:18.922276 | |
| // build LocalTime object manually | |
| time = LocalTime.of(9, 26, 18); | |
| System.out.println(time); // 09:26:18 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.time.LocalTime; | |
| public class Main { | |
| public static void main(String[] args) { | |
| LocalTime time = LocalTime.now(); | |
| System.out.println(time); // 09:26:18.922276 | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.time.LocalDate; | |
| import java.time.format.DateTimeFormatter; | |
| public class Main { | |
| public static void main(String[] args) { | |
| String str = "August 26, 2021"; | |
| DateTimeFormatter f = DateTimeFormatter.ofPattern("MMMM dd, uuuu"); | |
| LocalDate date = LocalDate.parse(str, f); |
NewerOlder