DEV Community

Cover image for 5 JAVA Classes no one knows about
archi-jain
archi-jain

Posted on

5 JAVA Classes no one knows about

In my most of the time, I focused heavily on finding the most efficient methods and classes that can be use in my projects. But the truth is, a lot of these methods are categorized into classes and libraries, and only by understanding their respective purposes will you be able to optimize your usage of its functions.
Throughout my coding experience, I came across several classes that I was never aware of, although I haven't mastered them yet, but still one should know about these...
The same classes will definitely make programming easier, faster, and more efficient for you.

1. The DoubleSummaryStatistics Class

Although initially confusing, the DoubleSummaryStatistics class is simply used to collect statistics. Information such as the average, the maximum, and the minimum can be stored.
Essentially this can function like an array; values can be accepted into the object, which is subsequently recorded. Data and information summaries can then be extracted from these values.

Basics

This class can be instantiated using a constructor. This constructor will be empty and provide no statistical information nor any values to the instance.

DoubleSummaryStatistics name = new DoubleSummaryStatistics()

Methods

  • getAverage() — returns the average of the values recorded
  • accept() — records another value into the object
  • getCount() — returns the count of values in the object
  • getSum() — returns the sum of the values recorded ###Example
DoubleSummaryStatistics c = new DoubleSummaryStatistics();
c.accept(1);
c.accept(2);
c.accept(3);
System.out.println("Average: " + c.getAverage());
System.out.println("Sum: " + c.getSum());
System.out.println("Count: " + c.getCount());
/*
Output:
Average: 2.0
Sum: 6.0
Count: 3
*/
Enter fullscreen mode Exit fullscreen mode

2. The TimeZone Class

The Timezone Class is used to get UTC time zone offsets. A UTC offset is the amount of time a certain time zone is ahead of or behind the Coordinated Universal Time (UTC).
It can also be used to acquire timezone IDs and figure out daylight savings. I find this class useful when designing time components for applications, or figuring out the location of users.

Basics

When working with individual, specific timezones, you want to instantiate a timezone object that represents a particular time zone. To create a timezone object, instead of a contractor you use the getTimeZone() method. This method is one of the methods belonging to the TimeZone class, and it returns a TimeZone object corresponding to the ID entered within the parameter.
Syntax:

TimeZone timezoneName = TimeZone.getTimeZone(timezoneID);

Example:

TimeZone tokyo = TimeZone.getTimeZone("Asia/Tokyo");

If you do not know the ID for a particular region’s time zone, here is the general formula for the ID.
"Continent/City"

Methods

Here are some other methods that were not used above.

  • getID() — returns the ID of the corresponding timezone object
  • getAvailableIDs() — returns an array containing all the available timezone IDs in this class
  • setID(ID) — sets a time zone ID to the string specified in the parameters (changes the ID of the time zone to a custom name)
  • getOffset(date) — returns the offset of the corresponding timezone object from UTC at the given date (specified in the parameter as a long number)
  • getDefault() — returns the default timezone of your region

Example:

TimeZone myRegion = TimeZone.getDefault();
System.out.println("Original Time Zone: " + myRegion.getID());
myRegion.setID("Africa/Tandrewland");
System.out.println("New Arrival: " + myRegion.getID());
/* 
Output:
Original Time Zone: America/Toronto
New Arrival: Africa/Tandrewland
*/
Enter fullscreen mode Exit fullscreen mode

3. The GregorianCalendar Class

The Gregorian Calendar Class is a concrete subclass of the Calendar class.
The Calendar class is an abstract class. A concrete subclass is a subclass of an abstract class.
It is a calendar that supports both Gregorian and Julian calendar systems. The Gregorian calendar is the calendar used in most of the world, a modification of the Julian Calendar enacted by Pope Gregory XIII. The Julian Calendar was invented by Julius Caesar and was based on the Roman calendar.

Basics

The way I create a GregorianCalendar object is simply through a constructor. The program will provide the calendar with the current date, time, and time zone. These values, however, can be customized through the constructor parameters.

Syntax:

GregorianCalendar name = new GregorianCalendar(date/time)
The date and time can be specified in this format:
year, month, dayOfMonth, hour, minute, and second
Note that this argument is optional. You can also pass the time zone, locale, or both as a parameter in the constructor.

Methods

Here are some methods in the Gregorian Calendar class.

  • getTime() — returns the current date and time of the calendar object. This may not be the actual current date and time depending on how you may have altered the calendar’s attributes.
  • setTime() — sets the date and time of the calendar object. This parameter takes in two parameters. The first is the calendar field to be altered (e.g. if you are setting the month, then you would specify this argument as Calender.MONTH). The second argument is the new altered value.
// Calendar representing current time and date
GregorianCalendar g = new GregorianCalendar();
System.out.println("Original Time: " + g.getTime());
// Setting the current day to the 28th
g.set(Calendar.DAY_OF_MONTH, 28);
System.out.println("Altered Time: " + g.getTime());
/* 
Output:
Original Time: Mon Sep 20 18:10:11 EDT 2021
Altered Time: Tue Sep 28 18:10:11 EDT 2021
*/
Enter fullscreen mode Exit fullscreen mode

4. The Date Class

The Date class allows access to date objects, which represent various dates and times, like a more specific version of the Calendar class.

Basics

A Date object can be created using a constructor.
Date name = new Date();
If Date() is used to create the object, then the object will contain data representing the current date and time. However, this can be customized by entering a long number into the Date() constructor. The long number stands for milliseconds. This constructor can also receive integer parameters representing the year, month, hours, and minutes. In the end, these are all optional arguments.

Methods

  • setTime() — sets the time of a date object. It takes in a single parameter: a long number representing the time in milliseconds. The date object to which that the method is applied will be changed.
  • getTime() — returns the time and date represented by the given date object in the form of milliseconds.
  • after() — Checks whether if the date of the date object is after the date passed into the argument.
  • before() — Opposite of after(). Checks whether if the date of the date object is before the date passed into the argument.

Example:

Date birth = new Date(2006, 2, 27);
Date death = new Date(2021, 4, 3);
Date current = new Date();

System.out.println("Is death after birth? " + death.after(birth));
System.out.println("Is birth before death? " + birth.before(death));
System.out.println("What is the current time? " + current.getTime());

current.setTime(100);

System.out.println("What is the altered time? " + current);
/*
Output:
Is death after birth? true
Is birth before death? true
What is the current time? 1632624244495
What is the altered time? Wed Dec 31 19:00:00 EST 1969
*/
Enter fullscreen mode Exit fullscreen mode

5. The Calendar Class

The Calendar class is an abstract class that is related to the Gregorian Calendar and Date class. They are often used in unison in order to optimize data manipulation in programs.

Basics

Since the Calendar class is an abstract class, a constructor cannot be used to create a Calendar object. Instead, we use the getInstance() method.
Calendar name = Calendar.getInstance();
By doing this, the variable assigned to the instance will be a calendar object containing data with the current time and date, much like the Date function. However, in the Calendar class, you get to work with specific Calendar fields: Month, Day, Hour, Year, and etc.

Methods

  • get() — returns the value of the calendar field specified in the parameter corresponding to the assigned calendar object.
  • getMaximum() — returns the maximum possible value of the given calendar field for the assigned calendar object

Example:

Calendar cal = Calendar.getInstance();

System.out.println("Current Month: " + cal.get(Calendar.MONTH));
// Max Days in Week:
System.out.println(cal.getMaximum(Calendar.DAY_OF_WEEK));
/*
Output:
Current Month: 8
Maximum amount of Days in a Week: 7
*/
Enter fullscreen mode Exit fullscreen mode

Conclusion

The classes discussed above are useful in a variety of scenarios that are common in modern-day applications. Regardless of whether if you know these classes or not, explore their methods, familiarize yourself with their usage, and put them into practice in your future projects. This will certainly set you on a path to optimizing your coding skills and further extending your knowledge. Thank you for reading this

Top comments (0)