How to get day from specified date in android

Suppose my date is 02-01-2013

and it is stored in a variable like:

String strDate = "02-01-2013";

then how should I get the day of this date (i.e TUESDAY)?

Upvotes: 4

Views: 21029

Answers (4)

I think that based on Android documentation is suggested the use of Calendar,

You need to be careful because the first day 1 is Sunday and the first month January Also check that you can get DAY_OF_WEEK, DAY_OF_MONTH etc

Upvotes: 0

Mohit Raval
Mohit Raval

Reputation: 438

use this format for date, day and time.

Date dNow = new Date(); SimpleDateFormat ft = new SimpleDateFormat("E yyyy.MM.dd 'at' hh:mm:ss a zzz");

and get out put of object here with format method. ft.format(dNow)

Upvotes: 1

jellyfication
jellyfication

Reputation: 1605

Use Calendar class from java api.

Calendar calendar = new GregorianCalendar(2008, 01, 01); // Note that Month value is 0-based. e.g., 0 for January.
int reslut = calendar.get(Calendar.DAY_OF_WEEK);
switch (result) {
case Calendar.MONDAY:
    System.out.println("It's Monday !");
    break;
}

You could also use SimpleDateFormater and Date for parsing dates

Date date = new Date();
SimpleDateFormat date_format = new SimpleDateFormat("yyyy-MM-dd");
try {
    date = date_format.parse("2008-01-01");
} catch (ParseException e) {
    e.printStackTrace();
}

calendar.setTime(date);

Upvotes: 9

Chinmoy Debnath
Chinmoy Debnath

Reputation: 2824

First split the string

String[] out = strDate.split("-");
                    s1 = Integer.parseInt(out[0]);
                    s2 = Integer.parseInt(out[1]) - 1;
                    yr = out[2];
char a, b, c, d;
                    a = yr.charAt(0);
                    b = yr.charAt(1);
                    c = yr.charAt(2);
                    d = yr.charAt(3);
                    s3 = Character.getNumericValue(a)*1000 +
                         Character.getNumericValue(b)*100 +
                         Character.getNumericValue(c)*10 +
                         Character.getNumericValue(d);

then create a calendar instance on that day

Calendar cal        = Calendar.getInstance();
cal.set(s3, s2, s1);

then get the day

cal.get(Calendar.DAY_OF_WEEK);

Upvotes: 3

Related Questions