There are many classes, functions, or objects (sometimes called utilities) that come built-in with each programming language that can be very useful. The java.util package contains many useful classes for everyday programming. Likewise, JavaScript and other languages come with many built-in objects for doing common tasks. I am going to cover a few of these.
Dates and Times
Java Date-Time
Java 8 introduced a new and improved date-time application program interface (API) in the java.time package that is much safer, easier to read, and more comprehensive than the previous API.
There’s also a LocalDateTime class to represent date and time, LocalTime to represent only time, and ZonedDateTime to represent a time with a time zone.
Date actually represents both a date and time.
Calendar is used to manipulate dates.
Groovy Date
A Category is a class that can be used to add functionality to other existing classes. In this case, TimeCategory adds a bunch of methods to the Integer class.
This is one of the many meta-programming techniques available in Groovy. To make a category, you create a bunch of static methods that operate on one parameter of a particular type (e.g., Integer). When the category is used, that type appears to have those methods. The object on which the method is called is used as the parameter. Take a look at the documentation for TimeCategory for an example of this in action.
JavaScript Date
JavaScript also has a built-in Date2 object.
Note that if you adhere to the international standard (yyyy-MM-dd), a UTC time zone will be assumed; otherwise, it will assume you want a local time.
As usual with JavaScript, the browsers all have slightly different rules, so you have to be careful with this.
Don’t ever use getYear! In both Java and JavaScript, the Date object’s getYear method doesn’t do what you think and should be avoided. For historical reasons, getYear does not actually return the year (e.g., 2014). You should use getFullYear() in JavaScript and LocalDate or LocalDateTime in Java.
Java DateFormat
Although DateFormat is in java.text, it goes hand in hand with java.util.Date.
This would format a date per the US standard: month/day/year.
Java 8 introduced the java.time.format.DateTimeFormatter to format or parse using the new date and time classes. Each java.time class, such as LocalDate, has a format method and a static parse method which both take an instance of DateTimeFormatter.
More Formatting See the documentation for SimpleDateFormat3 for more information about it. See the DateTimeFormatter4 documentation for more about it.
Currency
getInstance(Locale): Static method to get an instance of Currency based on Locale
getInstance(String): Static method to get an instance of Currency based on a currency code
getSymbol(): Currency symbol for the current locale
getSymbol(Locale): Currency symbol for the given locale
static getAvailableCurrencies(): Returns the set of available currencies
TimeZone
In Java 8 and above, time zones are represented by the java.time.ZoneId class . There are two types of ZoneIds, fixed offsets and geographical regions. This is to compensate for practices such as daylight saving time, which can be very complex.
Write a program that does this and run it. For example, in the groovyConsole, write the following and execute:
import java.time.*
println(ZoneId.getAvailableZoneIds())
Scanner
Scanner can be used to parse files or user input. It breaks the input into tokens, using a given pattern, which is whitespace by default (“whitespace” refers to spaces, tabs, or anything that is not visible in text).
Write a program that does this and try it out. Since this requires input, it can’t be done with the groovyConsole. Use either NetBeans to build a Java application or a Groovy script you run with groovy on the command line.