returns Date type casted to String for a given year, month, day to the calling method in format d,M,yyyy - Java java.util

Java examples for java.util:Month

Description

returns Date type casted to String for a given year, month, day to the calling method in format d,M,yyyy

Demo Code


//package com.java2s;
import java.text.DateFormat;

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

public class Main {
    public static void main(String[] argv) throws Exception {
        int year = 2;
        int month = 2;
        int day = 2;
        System.out.println(getDate(year, month, day));
    }//from w  ww .j a  v  a  2  s .c  o  m

    /**This method returns Date type casted to String
     * for a given year, month, day to the calling method
     * in format d,M,yyyy
     * @param year
     * @param month
     * @param day
     * @return
     * @throws ParseException
     */
    public static String getDate(int year, int month, int day)
            throws ParseException {
        Calendar cal = Calendar.getInstance();
        cal.clear();
        cal.set(Calendar.YEAR, year);
        cal.set(Calendar.MONTH, month);
        cal.set(Calendar.DAY_OF_MONTH, day);
        Date fromDate = new Date(cal.getTimeInMillis());
        DateFormat df = new SimpleDateFormat("d,M,yyyy");
        String formattedDate = df.format(fromDate);
        return formattedDate;
    }
}

Related Tutorials