Classes

Call a Private Constructor

Calling a private constructor from outside its class can be done via Reflection as follows: public final class Users { private Users() {} // static members}Class usersClass = Users.class;Constructor emptyUsersCnstr = usersClass.getDeclaredConstructor();emptyUsersCnstr.setAccessible(true);Users usersViaEmptyCnstr = emptyUsersCnstr.newInstance();

Employing the Formatter Class in Java

The Formatter class is used to format the output as needed. Here, we are trying to print the current time formatted in more than one format. Please see the output to understand further import java.util.*;public class FormatterEx{ public static void main(String args[]) { FormatterEx formatterEx = new FormatterEx(); formatterEx.proceed(); }

Merge Two Dictionaries in Python

The Dictionary class in Python provide an update() function that allows you to merge two dictionaries. For example: employeeScore = { ‘Emp1’:100, ‘Emp2’:98, ‘Emp3’:96}employee2019Score= { ‘Emp1’:101, ‘Emp2’:104} To merge, just use: employeeScore.update(employee2019Score) The contents of the employeeScore dictionary would be: employeeScore = { ‘Emp1’:101, ‘Emp2’:104, ‘Emp3’:96}

Usage of the Repository Factory Class

Spring Data provide a class named RepositoryFactorySupport. Via this class we can access any repository, as in the following example: RepositoryFactorySupport factory = … // code that instantiate the factory;CustomerRepository repo = factory.getRepository(CustomerRepository.class);

Get the UTC Machine Time in Java

Beginning with JDK 8, the machine UTC timestamp with nanoseconds precision can be obtained via the java.time.Instant class as below: // e.g., 2019-02-26T15:28:21.051163100ZInstant now = Instant.now();

Expore the TemporalAdjusters Class in Java

TemporalAdjusters is a class in Java that is oriented towards bettering the options of Calendar. You can easily compute the dates of firstDayOfNextMonth, lastDayOfMonth, next Wednesday, etc. TemporalAdjusters have many more methods defined and it is good to explore a few of them yourself. The API is available at https://docs.oracle.com/javase/8/docs/api/java/time/temporal/TemporalAdjusters.html. import java.time.LocalDate;import

Avoid Poor Use of Reflection

Class beanClass = ???If (beanClass.newInstance() instanceof TestBean) ??? The above code is tries to use the reflection API, it tries to find a way to check for inheritance, but it didn’t find a way to do it. So it just created a new instance and uses the instanceof operator. It is

Dealing with Datetimeoffset Serialization to Strings

DateTimeOffSet values are often converted to strings for display or some other purpose. But these representations often fail when they are used to convert them back to valid date/time values. We need to use the ToString method’s overload and pass the “u” or “s” formats as they are culture-invariant. For

Using Calendar.getInstance()

Calendar c = Calendar.getInstance(); c.set(2017, Calendar.OCTOBER, 29);  This code behave as a Gregorian calendar, but if the returned Calendar subclass is a Islamic, Julian, Buddistic or Hebrew calendar, then the month called October or the year 2017, doesn’t exist. Calendar.getInstance() uses the current default locale to select an appropiate implementation. The utility of Calendar.getInstance() is very limited  and it should be avoided because it’s results is not properly defined. Calendar c = new GregorianCalendar (timeZone); c.set(2017, Calendar.OCTOBER, 29); 

No more posts to show