Entries by Aliza

How to convert String to Date – Java?

In this tutorial, we will show you how to convert a String to java.util.Date. Many Java beginners are stuck in the Date conversion, hope this summary guide will helps you in some ways. // String -> Date SimpleDateFormat.parse(String); // Date -> String SimpleDateFormat.format(date); Refer to table below for some of the common date and time patterns […]

How to convert String to Date – Java?

In this tutorial, we will show you how to convert a String to java.util.Date. Many Java beginners are stuck in the Date conversion, hope this summary guide will helps you in some ways. // String -> Date SimpleDateFormat.parse(String); // Date -> String SimpleDateFormat.format(date); Refer to table below for some of the common date and time patterns […]

Java – How to get current date time?

In this tutorial, we will show you how to get the current date time from the classic Date and Calendar APIs, and also the new Java 8 date and time APIs – LocalDateTime and LocalDate 1. Code Snippets For java.util.Date, just create a new Date() DateFormat dateFormat = new SimpleDateFormat(“yyyy/MM/dd HH:mm:ss”); Date date = new Date(); System.out.println(dateFormat.format(date)); //2016/11/16 12:08:43 For java.util.Calendar, uses Calendar.getInstance() DateFormat […]

How to compare dates in Java?

Few examples show you how to compare two dates in Java. Updated with Java 8 examples. 1. Date.compareTo() A classic method to compare two java.util.Date in Java. Return value is 0 if both dates are equal. Return value is greater than 0 , if Date is after the date argument. Return value is less than 0, if […]

Java 8 – How to Convert Instant to LocalDateTime?

Java 8 examples to show you how to convert from Instant to LocalDateTime 1. Instant -> LocalDateTime The java.time.LocalDateTime has no concept of time zone, just provide a zero offset UTC+0.InstantExample1.java package com.mkyong.date; import java.time.Instant; import java.time.LocalDateTime; import java.time.ZoneOffset; public class InstantExample1 { public static void main(String[] argv) { // Parse a ISO 8601 Date directly //Instant instant = Instant.parse(“2016-08-18T06:17:10.225Z”); […]