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)?
Solution 1:
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
Datedate=newDate();
SimpleDateFormatdate_format=newSimpleDateFormat("yyyy-MM-dd");
try {
date = date_format.parse("2008-01-01");
} catch (ParseException e) {
e.printStackTrace();
}
calendar.setTime(date);
Solution 2:
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);
Solution 3:
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)
Post a Comment for "How To Get Day From Specified Date In Android"