Add Hours To a Date In Java 1. Overview Before Java 8, java.util.Date was one of the most commonly used classes for representing date-time values in Java. Then Java 8 introduced java.time.LocalDateTime and java.time.ZonedDateTime. Java 8 also allows us to represent a specific time on the timeline using java.time.Instant. In this tutorial,… Continue Reading java-add-hours-date

Format ZonedDateTime to String 1. Overview In this quick tutorial, we’ll see how to convert a ZonedDateTime to a String. We’ll also look at how to parse a ZonedDateTime from a String. 2. Creating a ZonedDateTime First, we’ll start with a ZonedDateTime with a time zone of UTC. There are several… Continue Reading java-format-zoned-datetime-string

Guide to DateTimeFormatter 1. Overview In this tutorial, we’ll review the Java 8 DateTimeFormatter class and its formatting patterns. We’re also going to discuss possible use cases for this class. We can use DateTimeFormatter to uniformly format dates and times in an app with predefined or user-defined patterns. 2. DateTimeFormatter with Predefined Instances DateTimeFormatter… Continue Reading java-datetimeformatter

Convert Between java.time.Instant and java.sql.Timestamp 1. Overview Both java.time.Instant and java.sql.Timestamp classes represent a point on the timeline in UTC. In other words, they represent the number of nanoseconds since the Java epoch. In this quick tutorial, we’ll convert one to the other by using built-in Java methods. 2. Converting Instant to Timestamp and Back We… Continue Reading java-time-instant-to-java-sql-timestamp

Converting Between LocalDate and SQL Date 1. Overview In this quick tutorial, we’ll learn how to convert between java.time.LocalDate and java.sql.Date. 2. Direct Conversion To convert from LocalDate to java.sql.Date, we can simply use the valueOf() method available in *java.sql.Date*. Likewise, to convert the current date, we can use: Date date… Continue Reading java-convert-localdate-sql-date

Convert Time to Milliseconds in Java 1. Overview In this quick tutorial, we’ll illustrate multiple ways of converting time into Unix-epoch milliseconds in Java. More specifically, we’ll use: Core Java’s java.util.Date and Calendar Java 8’s Date and Time API Joda-Time library 2. Core Java ==== 2.1. Using Date Firstly, let’s… Continue Reading java-time-milliseconds