Friday, 3 June 2022

Get Date object from year, month and day

Below snippet return the Date object from the given year, month and day.

private static Date getDate(Integer year, Integer month, Integer day) throws ParseException {
	String dateStr = year + "-" + month + "-" + day;
	SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
	return formatter.parse(dateStr);
}

 


 

Find the below working application.

 

DateUtil.java

package com.sample.app;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateUtil {

	private static Date getDate(Integer year, Integer month, Integer day) throws ParseException {
		String dateStr = year + "-" + month + "-" + day;
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		return formatter.parse(dateStr);
	}

	public static void main(String[] args) throws ParseException {
		Date date = getDate(2022, 6, 6);

		System.out.println(date);
	}

}

Output

Mon Jun 06 00:00:00 IST 2022


  

Previous                                                 Next                                                 Home

No comments:

Post a Comment