Create Date From year and month - Android java.util

Android examples for java.util:Year

Description

Create Date From year and month

Demo Code

import android.annotation.SuppressLint;
import android.util.Log;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class Main{

    public static Date getDateFromString(int year, int month) {
        String dateString = year + "-"
                + (month > 9 ? month : ("0" + month)) + "-01";
        Date date = null;/*from  ww w .  j  a  v  a  2s .c  om*/
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            date = sdf.parse(dateString);
        } catch (ParseException e) {
            System.out.println(e.getMessage());
        }
        return date;
    }

}

Related Tutorials