开发者

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

开发者 https://www.devze.com 2023-01-25 02:29 出处:网络
I have the following code in Objective-C for the iPhone, using NSDateFormatter: NSDate *today = [NSDate date];

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

精彩评论

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