get Day For Date as int value - Android java.util

Android examples for java.util:Day

Description

get Day For Date as int value

Demo Code


//package com.java2s;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class Main {

    public static int getDayForDate(String dateStr) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date date;//from   w w  w  . j a v  a 2  s .c  o  m
        int day = 0;
        try {
            date = format.parse(dateStr);
            Calendar cal = Calendar.getInstance();
            cal.setTime(date);
            day = cal.get(Calendar.DAY_OF_MONTH);

        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return day;
    }

    public static int getDayForDate(Date date) {
        int day = 0;
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        day = cal.get(Calendar.DAY_OF_MONTH);

        return day;
    }
}

Related Tutorials