从字符串开始的Java星期几 [英] Java day of the week from string

查看:134
本文介绍了从字符串开始的Java星期几的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个简单的代码:

  SimpleDateFormat format = new SimpleDateFormat(yyyy-MM-dd); 
日期日期= format.parse(2011-10-29);
calendar.setTime(date);
Log.d(Debug,星期几=+(calendar.get(Calendar.DAY_OF_WEEK)== Calendar.SATURDAY));

10月29日是星期六,为什么我会弄错?

解决方案

这是一个如何发生这种情况的例子......

  SimpleDateFormat format = new SimpleDateFormat(yyyy-MM-dd); 
Date date = null;
try {
date = format.parse(2011-10-29);
} catch(ParseException e){
e.printStackTrace();
}
日历日历= Calendar.getInstance(TimeZone.getTimeZone(GMT));
calendar.setTime(date);
System.out.println(星期几=
+(calendar.get(Calendar.DAY_OF_WEEK)));
System.out.println(星期六?
+(calendar.get(Calendar.DAY_OF_WEEK)== Calendar.SATURDAY));

try {
date = format.parse(2011-10-29);
} catch(ParseException e){
e.printStackTrace();
}
calendar = Calendar.getInstance(TimeZone.getTimeZone(PST));
calendar.setTime(date);
System.out.println(星期几=
+(calendar.get(Calendar.DAY_OF_WEEK)));
System.out.println(星期六?
+(calendar.get(Calendar.DAY_OF_WEEK)== Calendar.SATURDAY));

输出

 星期六= 7 
星期六?真的
星期几= 6
星期六? false

所以是的,取决于你所在的时区将是或不会是星期六。 / p>

I have this simple code:

SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Date date = format.parse("2011-10-29");
calendar.setTime(date);
Log.d("Debug","Day of the week = "+(calendar.get(Calendar.DAY_OF_WEEK)==Calendar.SATURDAY));

The 29th of October is a Saturday so why do I get false?

解决方案

Here is an example of how this could happen...

    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
    Date date = null;
    try {
        date = format.parse("2011-10-29");
    } catch (ParseException e) {
        e.printStackTrace();
    }
    Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
    calendar.setTime(date);
    System.out.println("Day of the week = "
            + (calendar.get(Calendar.DAY_OF_WEEK)));
    System.out.println("Saturday? "
            + (calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY));

    try {
        date = format.parse("2011-10-29");
    } catch (ParseException e) {
        e.printStackTrace();
    }
    calendar = Calendar.getInstance(TimeZone.getTimeZone("PST"));
    calendar.setTime(date);
    System.out.println("Day of the week = "
            + (calendar.get(Calendar.DAY_OF_WEEK)));
    System.out.println("Saturday? "
            + (calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY));

which outputs

Day of the week = 7
Saturday? true
Day of the week = 6
Saturday? false

so yes, depending on what time zone you are in it will or will not be Saturday.

这篇关于从字符串开始的Java星期几的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆