4

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)?

Hussein El Feky
  • 6,627
  • 5
  • 44
  • 57

4 Answers4

9

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);
jellyfication
  • 1,595
  • 1
  • 16
  • 37
3

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);
Chinmoy Debnath
  • 2,814
  • 16
  • 20
1

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)

Mohit Raval
  • 440
  • 1
  • 6
  • 19
0

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