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”. It is very important to understand difference between them to correctly utilize these constants for converting date or time in Eastern Standard Time values.
Learn to create List instances with only one element in it using Arrays.asList() and Collections.singletonList() methods. Using Collections.singletonList() Method [ Immutable List ] This is simplest and recommended method to create immutable List with single element inside it. The list created with this method is immutable as well, so you are sure that there will […]
Learn to append content to file in java using BufferedWritter, PrintWriter, FileOutputStream and Files class. In all the examples, while opening the file to write, you have pass a second argument as true which denotes that file is opened in append mode. Table of Contents Append to File using BufferedWritter Append to File using PrintWriter […]
When working on enterprise application, sometimes it is needed to write files in java e.g. writing reports in filesystem.. Though, there are multiple ways to do so, let’s quickly go through few of them for quick reference when it is needed.
Java comments, as name suggests, are notes you write between the programs for various reasons. For example, you may write comments to – write information or explanation about the variable, method, class or any statement. write text to be available in Java docs. hide program code for specific time, etc. Types of Java Comments There […]