Java examples to calculate the duration or difference between two date instances (Date and LocalDate classes). Duration between two LocalDate instances Use Duration.between() method to get the difference between two local date instances. Difference between two Date instances Use Duration.between() method to get the difference between two date instances. Happy Learning !! Duration Javadoc LocalDate […]
Java examples to use DateTimeFormatter for formatting ZonedDateTime, LocalDateTime, LocalDate and LocalTime to String with predefined and custom patterns.
Java 8 examples to convert from LocalDate to java.sql.Date and vice versa.
Java examples to convert from LocalTime to java.sql.Time and vice versa.
Java example to convert LocalDate to Date and vice-versa. 1) Date to LocalDate Java example to convert from Date to LocalDate. 2) LocalDate to Date Java example to convert from LocalDate to Date. DateUtils Class Utility class with simple method class to convert between Date and LocalDate variants. How to use Happy Learning !! References: […]
Java example to convert LocalDateTime to Date and vice-versa. 1) Date to LocalDateTime Java example to convert from Date to LocalDateTime. 2) LocalDateTime to Date Java example to convert from LocalDateTime to Date. DateUtils Class Utility class with simple method class to convert between Date and LocalDateTime variants. How to use Happy Learning !! References: […]
Java example to convert LocalDateTime to ZonedDateTime and vice-versa. 1) LocalDateTime to ZonedDateTime ZonedDateTime = LocalDateTime + ZoneId Output: 2) ZonedDateTime to LocalDateTime Use ZonedDateTime.toLocalDateTime() method to get local datetime without timezone information. Output: Happy Learning !! References: ZonedDateTime Javadoc LocalDateTime Javadoc ZoneId Javadoc
Java examples to convert a date from one timezone to another timezone. We will see the examples using ZonedDateTime, Date and Calendar classes.
Java examples to add or substract days, months or years from java.util.Date and java.time.LocalDateTime classes.
Java example to format LocalDateTime instance to String using DateTimeFormatter class.
Learn to calculate execution time or elapsed time of program or some statements using System.nanoTime() or System.currentTimeMillis() methods.
Learn to convert string to java.time.LocalDate object in Java. LocalDate instances are immutable and thread-safe, which makes it very useful for robust application design.
Learn to parse dates from string to java.util.Date object. Also see some useful date pattern strings, which will help you in building your own custom date pattern. Useful Date Patterns Pattern Example yyyy-MM-dd (ISO) “2018-07-14” dd-MMM-yyyy “14-Jul-2018” dd/MM/yyyy “14/07/2018” E, MMM dd yyyy “Sat, Jul 14 2018” h:mm a “12:08 PM” EEEE, MMM dd, yyyy […]
In Java, timestamps should be represented with java.time.Instant from Java 8, and java.sql.Timestamp till Java 7. Learn to get current timestamp in java.
Learn to compare dates using Date (till Java 7); LocalDate, LocalTime and LocalDateTime classes from Java 8. Date Comparison in Java 8 Date Classes java.time.LocalDate, java.time.LocalTime and java.time.LocalDateTime. Methods date1.isAfter( date2 ) – It returns true is date1 comes after date2; else false. date1.isBefore( date2 ) – It returns true is date1 comes before date2; […]
Java supports three timezone constants for Eastern Standard Time i.e. “EST”, “America/New_York” and “EST5EDT”. Learn to convert date to EST in Java.
Learn to convert XMLGregorianCalendar to Date class or string value. Also learn to apply timezone changes and daylight saving effects as well.
In this example, we will learn to format XMLGregorianCalendar class to string in multiple patterns e.g. ‘MM/dd/yyyy hh:mm a z’. XMLGregorianCalendar to String in Date/TimeStamp Pattern In below code sample, I am creating instance of XMLGregorianCalendar with current Date object. Then I am formatting it to String format in given pattern. Output: 01/27/2017 01:32 PM […]
In java, java.util.Locale class represents a specific language and region of the world. If a class varies its behavior according to Locale, it is said to be locale-sensitive. Learn more.
Learn to create new date, get current date, parse date to string or format Date object using java.util.Date class. These use-cases are frequently required, and having them in one place will help in saving time for many of us.