Introduction
Java language supports working with date and time, formating dates, and resource bundles. Java 8 provides a lot of new libraries from java.time packages, earlier classes were from Date and Calendar. The later classes were mostly deprecated due to issues in i18n & localization.
Date – Time
LocalDate
- LocalDate.now()
- LocalDate.of(2021,12,21)
- LocalDate.parse(“2021-12-21”)
- localDate.plusWeeks(1)
- localDate.withYear(2021)
LocalTime
- LocalTime.now()
- LocalTime.of(9,5,23)
- LocalTime.parse(“13:23:23”)
- localTime.minus(3, ChronoUnit.MINUTES)
LocalDateTime
- LocalDateTime.now()
- LocalDateTime.parse(“2017-08-21 10:19”, DateTimeFormatter.ofPattern(“yyyy-MM-dd HH:mm”))
- LocalDateTime.of(date, time)
- localDateTime.plusDays(2)
- localDateTime.plusHours(2)
- localDate.plus(Duration.ofMinutes(5))
ZonedDateTime
- ZonedDateTime.now()
- ZonedDateTime.of(localDateTime, ZoneId.of(zoneString))
- ZonedDateTime.parse(“2021-05-05T10:12:23+05:30”)
- ZonedDateTime.of(2021, 12, 8, 12, 32, 55, 0, ZonedId.of(“US/Central”) )
- zonedDateTime.toInstant()
- zonedDateTime.with(TemporalAdjusters.next(DayOfWeek.THURSDAY))
- zonedDateTime.withZoneSameInstant(ZoneId.of(“US/Central”))
- zonedDateTime1.isBefore(zonedDateTime2)
ZonedId
- ZoneId.of(“US/Pacific”)
- ZonedId.getAvailableZoneIds()
- zoneId.getRules()
- zonedId.getRules().isDaylightSavings(instant)
Periods
- Period.ofMonths(1)
- zonedDateTime.minus(period)
Durations
- Duration.ofMinutes(ChronoUnit.MINUTES.between(localtime1, localtime2))
- localTime.plus(duration)
Instants
Instant is the number of seconds and nanoseconds since January 1, 1970(Java epoch)
- Instant.now()
- zonedDateTime.toInstant()
Locale
- Locale(“it”)
- Locale(“it”, “CH”)
- zonedDateTime.format(DateTimeFormater.ofLocalizedDateTime(FormatStyle.LONG).withLocale(Locale.JAPAN))
Properties
Used to Store Configuration settings and operating parameters for your application.
package come.jones.time;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Properties;
public class CustomProperties {
public static void main(String[] args) {
Properties prop = new Properties();
try {
FileInputStream fileInputStream = new FileInputStream("src/prop1.properties");
prop.load(fileInputStream);
prop.setProperty("prop3", "value3");
prop.list(System.out);
FileOutputStream fileOutputStream = new FileOutputStream("prop2.properties");
prop.store(fileOutputStream, "Adding prop3");
fileInputStream.close();
fileOutputStream.close();
System.out.println("End");
} catch (Exception e) {
}
}
}
Resource Bundles
Resource Bundles are helpful in providing language and country specific strings for display. When property files are used as resource bundles, then these files must end in “.properties”, and start with Locale, For Ex: MyApp_in_en.properties, MyApp_fr.properties.
ResourceBundle.getBundle(“APP”, new Locale(“en”, “CA”))
Choosing the correct properties file follows this Order.
- RB_en_CA.java/ properties –exactly what we need
- RB_en.java/properties –atleast language
- RB_hi_IN.java/properties — default Locale
- RB_hi.java/properties –atleast default language
- RB.java –atleast –atleast default Bundle
Spot on with this write-up, I seriously believe that this website needs far more attention. I’ll probably be back again to read more, thanks for the advice!