Create Date from year month day via yyyy-MM-dd format - Android java.util

Android examples for java.util:Year

Description

Create Date from year month day via yyyy-MM-dd format

Demo Code

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

public class Main{

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

}

Related Tutorials