<< July 2010 | Home | September 2010 >>

Friday Java Quiz: Know Your Calendars And Dates

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.

Oracle Sues Google Over Android

Oh boy! Is this real?

San Francisco Chronicle:

(08-12) 17:17 PDT Redwood Shores, Calif. (AP) --

Oracle Corp. said Thursday it has filed a patent and copyright-infringement lawsuit against Google Inc.

Oracle said in a statement that Google's Android system for mobile phones infringes on its patented Java technology.

This is the other shoe dropping in a story I reported 1221 days ago:

Here's an excerpt of a couple of comments from that post:

Re: Java: The Third Fork From Sun

Are you sure that it won't prevent ASF to release Harmony under the Harmony name without references to Java/JVM ?

As far as I understood it you are not granted patent licensing if you don't pass the JCK, so, I guess you are wrong in that statement.

Re: Java: The Third Fork From Sun

I'm speculating that Sun will not make any patent actions against Harmony even though the letter of the spec allows them that option. The PR backlash for such a thing would be unbearable.

A precedence of this sort of a situation is the relationship between Microsoft and Mono.

Things may play out differently, though.

Is this the end of Java as we know it?

QOTD: Currently plain Java is my favorite ...

... IOC container.

Bill Burke (commenting on TheServerSide.com): Currently plain Java is my favorite IoC container. Nothing beats the simplicity of constructor and getter/setter methods. I'm so sick of seeing the guts of applications exposed through ugly XML or writing a billion annotations just to get access to a simple component. Its all ridiculous, bloated, and most of the time unnecessary. Anybody else fed up?

I know a lot of people who has sentiments like Bill's. I can just hear them say:

  • My favorite OR mapping tool is JDBC
  • My favorite quote-and-quote application server is the Java process
  • My favorite logging solution is java.logging API
  • My favorite application modeling tool are Java interfaces
  • My favorite web application framework is none of the usual candidates
  • My favorite JVM language is Java

And, yes, Bill, we are all fed up with writing stupid XML files!

Tags :