Date and Time

How to convert String to Date in Java in yyyy-mm-dd format

Table of Contents

Program

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.DayOfWeek;
import java.util.Calendar;
public class DateWorkTask 
{
	public static void main(String[] args)
	{
		LocalDate currentDate = LocalDate.now();
		System.out.println("Current Date with yyyy MM dd Pattern = " + currentDate);
		String formattedDate2 = currentDate.format(DateTimeFormatter.ofPattern("dd-MM-yyyy"));
		System.out.println("Date with pattern dd MM yyyy :  " + formattedDate2);
		DayOfWeek day = currentDate.getDayOfWeek();
		String weekName = day.name();
		int weekVal = day.getValue();
		System.out.println("Week NO: " + weekVal);
		System.out.println("DAY: " + weekName);
		Calendar cal = Calendar.getInstance();
		String[] monthName = { "January", "February", "March", "April", "May", "June", "July", "August", "September","October", "November", "December" };
		String month = monthName[cal.get(Calendar.MONTH)];
		System.out.println("Month name: " + month);
		System.out.println("Month = " + (cal.get(Calendar.MONTH) + 1));
		System.out.println("Current Date UTC = " + cal.getTime());
		System.out.println("Hour:" + cal.get(Calendar.HOUR_OF_DAY) + ":" + cal.get(Calendar.MINUTE));
		System.out.println("Hour in Milliseconds =" + cal.getTimeInMillis());
	}
}

Output

Current Date with yyyy MM dd Pattern = 2021-04-09
Date with pattern dd MM yyyy :  09-04-2021
Week NO: 5
DAY: FRIDAY
Month name: April
Month = 4
Current Date UTC = Fri Apr 09 14:49:50 IST 2021
Hour:14:49
Hour in Milliseconds =1617959990268

Description

public static LocalDate now()

Obtains the current date from the system clock in the default time-zone.
This will query the system clock in the default time-zone to obtain the current date.
Using this method will prevent the ability to use an alternate clock for testing because the clock is hard-coded.

Returns:

the current date using the system clock and default time-zone, not null

public static DateTimeFormatter ofPattern(String pattern)

Creates a formatter using the specified pattern.
This method will create a formatter based on a simple pattern of letters and symbols as described in the class documentation. For example, d MMM uuuu will format 2011-12-03 as ‘3 Dec 2011’.

The formatter will use the default FORMAT locale. This can be changed using withLocale(Locale) on the returned formatter Alternatively use the ofPattern(String, Locale) variant of this method.

The returned formatter has no override chronology or zone. It uses SMART resolver style.

Parameters:

pattern – the pattern to use, not null

Returns:

the formatter based on the pattern, not null

Throws:

IllegalArgumentException – if the pattern is invalid

public int getValue()

Gets the day-of-week int value.
The values are numbered following the ISO-8601 standard, from 1 (Monday) to 7 (Sunday). See WeekFields.dayOfWeek() for localized week-numbering.

Returns:

the day-of-week, from 1 (Monday) to 7 (Sunday)

public static Calendar getInstance()

Gets a calendar using the default time zone and locale. The Calendar returned is based on the current time in the default time zone with the default locale.

Returns:

a Calendar.

public static final int MILLISECOND

Field number for get and set indicating the millisecond within the second. E.g., at 10:04:15.250 PM the MILLISECOND is 250.

public static final int HOUR_OF_DAY

Field number for get and set indicating the hour of the day. HOUR_OF_DAY is used for the 24-hour clock. E.g., at 10:04:15.250 PM the HOUR_OF_DAY is 22.

public static final int MONTH

Field number for get and set indicating the month. This is a calendar-specific value. The first month of the year in the Gregorian and Julian calendars is JANUARY which is 0; the last depends on the number of months in a year.