The Artima Developer Community
Sponsored Link

Java Buzz Forum
Friday Java Quiz: Know Your Calendars And Dates

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Weiqi Gao

Posts: 1808
Nickname: weiqigao
Registered: Jun, 2003

Weiqi Gao is a Java programmer.
Friday Java Quiz: Know Your Calendars And Dates Posted: Aug 27, 2010 11:42 PM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by Weiqi Gao.
Original Post: Friday Java Quiz: Know Your Calendars And Dates
Feed Title: Weiqi Gao's Weblog
Feed URL: http://www.weiqigao.com/blog/rss.xml
Feed Description: Sharing My Experience...
Latest Java Buzz Posts
Latest Java Buzz Posts by Weiqi Gao
Latest Posts From Weiqi Gao's Weblog

Advertisement

Once again, a late night Friday (Fri Aug 27 23:53:11 CDT 2010) Java quiz. This one arise out of work with Brian Coyner.

Q: Will the following Java program compile? Run without exceptions? If so, what does it print?

import java.util.Calendar; 
import java.util.Date; 
 
public class Main { 
    public static void main(String[] args) { 
        Date date = new Date(); 
        Date date2 = foo(date); 
        Date date3 = bar(date); 
        System.out.println("date2.getTime() == date3.getTime() = " + 
                (date2.getTime() == date3.getTime())); 
    } 
 
    private static Date foo(Date date) { 
        Calendar cal = Calendar.getInstance(); 
        cal.setTime(date); 
        cal.clear(Calendar.YEAR); 
        cal.clear(Calendar.MONTH); 
        cal.clear(Calendar.DATE); 
        cal.clear(Calendar.MILLISECOND); 
        return cal.getTime(); 
    } 
 
    private static Date bar(Date date) { 
        Calendar cal = Calendar.getInstance(); 
        cal.setTime(date); 
        Calendar cal2 = Calendar.getInstance(); 
        cal2.clear(); 
        cal2.set(Calendar.HOUR_OF_DAY, cal.get(Calendar.HOUR_OF_DAY)); 
        cal2.set(Calendar.MINUTE, cal.get(Calendar.MINUTE)); 
        cal2.set(Calendar.SECOND, cal.get(Calendar.SECOND)); 
        return cal2.getTime(); 
    } 
}

Lenient rules apply today: You are allowed to consult the JDK Javadocs and Google the internet. You are allowed to study the source code that comes with the JDK. However you do need to give your answer before you attempt to actually compile and run the program.

Read: Friday Java Quiz: Know Your Calendars And Dates

Topic: From Reel Life to Real Life: Pre-Crime Intelligence Software Previous Topic   Next Topic Topic: Orbcomm says Caterpillar renews satellite communications contract

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use