开发者

What is the Java (Android) equivalent for Cocoa's NSDateFormatter?

I have the following code in Objective-C for the iPhone, using NSDateFormatter:

    NSDate *today = [NSDate date];
NSDateFormatter *dateFormat = [[[开发者_如何学CNSDateFormatter alloc] init]autorelease];
[dateFormat setDateFormat:@"yyyy-MM-dd"];
NSString *dateString = [dateFormat stringFromDate:today];
if ([dateString isEqual:@"2010-11-12"]) {

What would be the equivalent to the NSDateFormatter used there in Java on Android? I'm still fairly new to Java and Android development.


Pure java code equivalent is:

        Date today = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String dateString = dateFormat.format(today);
        if (dateString.equals("2010-11-12")) {

        }

But why are you formatting the date and comparing it against a string instead of checking the date object itself?


final Date now = new Date();
final Format formatter = new SimpleDateFormat("yyyy-MM-dd");
if (formatter.format(now).equals("2010-11-12")) {
    // Do something
}

Note, the default constructor of java.util.Date initializes it to the current time

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜