How to get day from specified date in android

不羁的心 提交于 2019-11-28 02:02:48

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

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

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)

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!