How to get a day from a specified date in Android

Suppose my date is 02-01-2013

and it is stored in a variable, for example:

String strDate = "02-01-2013"; 

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

+4
source share
4 answers

Use Calendar class from java api.

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

You can 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); 
+5
source

First split the line

 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 an instance of the calendar that day

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

then get the day

 cal.get(Calendar.DAY_OF_WEEK); 
+2
source

Use this format for date, day, and time.

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

and exit the object here using the format method. ft.format (dNow)

+1
source

I think based on Android documentation it is suggested to use Calendar,

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

0
source

All Articles