Skip to main content

Posts

Showing posts from November, 2019

Convert LocalDate to Date in Java

This post shows how to convert java.time.LocalDate to java.util.Date in Java. For converting LocalDate to Date steps are as follows- Get the ZonedDateTime from the LocalDate by specifying the ZoneId. Convert that ZonedDateTime to Instant instance using toInstant() method. Pass instant to Date.from() method to get a java.util.Date instance. If we have to write those steps elaborately then it can be done as follows- LocalDate ld = LocalDate.now(); System.out.println("Local Date - " + ld); ZonedDateTime zdt = ld.atStartOfDay(ZoneId.systemDefault()); Instant instant = zdt.toInstant(); Date date = Date.from(instant); System.out.println("Date- " + date); Output Local Date - 2019-11-20 Date- Wed Nov 20 00:00:00 IST 2019 You can also do it in one line as given below- LocalDate ld = LocalDate.now(); Date date = Date.from(ld.atStartOfDay(ZoneId.systemDefault()).toInstant()); That's all for the topic Convert LocalDate to Date in Java . If somethin

Java HashSet With Examples

HashSet in Java is part of the Java Collections framework and one of the most used Set implementation in Java applications. HashSet class extends AbstractSet class and implements Set , Cloneable and Serializable interfaces. HashSet is backed by a HashMap instance that means HashSet class internally uses HashMap to store its elements. Table of contents Features of HashSet in Java Java HashSet constructors Capacity and Load Factor for HashSet Java example creating a HashSet HashSet class methods Duplicates are not allowed in HashSet Inserting Null is allowed in HashSet Example code to remove elements from HashSet Java HashSet iterator example HashSet is not threadsafe Features of HashSet in Java Some of the features of the HashSet which are discussed in this post are as follows- HashSet stores only unique elements. HashSet is an unordered collection, which means insertion order is not maintained as in ArrayList . HashSet in

Java Pass by Value or Pass by Reference

This post is about one of the basic question whether Java is pass by value or pass by reference . Though basic, this question causes some confusion mainly for objects passed as parameter. In this post let’s go through some examples to clarify the doubts and have a clear understanding of the topic. To start with first have an understanding of the two terms used here pass by value and pass by reference. Pass by value - In pass by value the value assigned to the variable is copied to another variable. That another variable uses the copied value with in its scope without modifying the original value. Pass by reference - In pass by reference the alias to an original variable is passed to the method parameter. Table of contents Java is pass by value Passing primitive as method parameter Passing object as method parameter Example code showing object fields can be changed Example code showing object reference can’t be changed Java is pass by value In Ja

Compare Dates in Java

In this post there are Java programs to compare dates in Java, options you have are using Date class methods, Calendar class methods and from Java 8 using methods in LocalDate , LocalTime and LocalDateTime classes. Table of contents Comparing java.util.Date instances Comparing java.util.Calendar instances Comparing LocalDates in Java Comparing java.util.Date If you have two Date instances and you want to compare them then the methods in the Date class that can be used are- compareTo(Date anotherDate) - Compares two Dates for ordering. Returns 0 if the argument Date is equal to this Date; a value less than 0 if this Date is before the passed Date argument; and a value greater than 0 if this Date is after the Date argument. equals(Object obj) - Compares two dates for equality. The result is true if and only if the argument is not null and is a Date object that represents the same point in time, to the millisecond, as this object. after(Date when) - Test

final in Java With Examples

final keyword in Java can be used with a field, method or class. In whichever context you use final in Java, it restricts the access in some way. Final variable – If a field is declared as final its value can’t be changed once assigned. Final method – A method declared as final can’t be overridden. Final class – A class declared as final can’t be extended. Table of contents Final variable in Java Examples of final field in Java Final variable in a method Final variable holding object reference Final object reference Java example code Final Method in Java Final class in Java Final variable in Java When a field is declared as final it can’t be modified thus making it a constant . Since a final variable is constant it is considered good practice to give final variable name in all uppercase. Since the value of a final variable can’t be modified so final variable can be initialized only once . That initialization can be done in follo

How to Append to a File in Java

In the post How to Write to a File in Java examples are already given to write to a file in Java but the examples shown there would create a new file or overwrite existing file when writing. Sometimes you may need to append to an existing file rather than overwriting it so this post shows options to append content to a file in Java. In Java there are two classes FileWriter and FileOutputStream for writing to a file. Where FileWriter is meant for writing streams of characters and FileOutputStream is used for writing streams of raw bytes. Both of these classes have a constructor with a boolean argument. When that boolean argument is passed as true then the content will be written to the end of the file . For example – public FileWriter(File file, boolean append); You should always use the wrapper classes BufferedWriter and BufferedOutputStream rather than using these classes directly. Apart from these classes Files class added in Java 7 also provides methods for appen