Showing posts with label Date. Show all posts
Showing posts with label Date. Show all posts

Monday, November 23, 2020

How To Convert String to Date in Java 8

1. Overview


In this article, you'll be learning how to convert String to Date in Java 8 as well as with Java 8 new DateTime API.

String date conversion can be done by calling the parse() method of LocalDate, DateFormat and SimpleDateFormat classes.

LocalDate is introduced in java 8 enhancements.

DateFormat is an abstract class and direct subclass is SimpleDateFormat.

To convert a String to Date, first need to create the SimpleDateFormat or LocalDate object with the data format and next call parse() method to produce the date object with the contents of string date.

Java Convert String to Date (Java 8 LocalDate.parse() Examples)


First, let us write examples on string to date conversations with SimpleDateFormat classs and then next with DateFormat class.

At last, Finally with LocalDate class parse() method.

All of the following classes are used to define the date format of input string. Simply to create the date formats.

LocalDate
DateFormat
SimpleDateFormat

Note: In date format Captial M indicates month whereas lowercase m indicate minutes.

This will be asked in the interview or written test on How to Format Date to String in Java 8?

Friday, February 21, 2020

Java 8 – TemporalAdjusters examples

1. Overview


In this tutorial, We'll learn about the new Java 8 TemporalAdjusters class and its methods along with examples.

This is part of the new java 8 date and time api and in java.time.temporal package and class name TemporalAdjusters. This class now has predefined temporal objects to change the data as we want.

TemporalAdjusters class is a collection of TemporalAdjuster implementations.

First We will see the example programs on TemporalAdjusters class and next take a look at the Temporal interface.

At last, We'll define custom TemporalAdjuster implementation.

Java 8 – TemporalAdjusters examples


Tuesday, April 9, 2019

java.util.Date in Java Example

java.util.Date in Java Example

Date class is inbuilt in Java API and package is java.util.Date.

Date class is used to get the current date, time or specific date time. This has methods to format and parse string dates. Implements Serializable, Cloneable, Comparable<Date>.


java.util.Date in Java Example

Constructor of Date class:


public Date()

Example program to get the current date or today's date: