Tag Archives: java

Most idiomatic way to print a time difference in Java?

Questions: I’m familiar with printing time difference in milliseconds: long time = System.currentTimeMillis(); //do something that takes some time… long completedIn = System.currentTimeMillis() – time; But, is there a nice way print a complete time in a specified format (eg: HH:MM:SS) either using Apache Commons or even the dreaded platform API’s Date/Time objects? In other… Read More »

Is it possible to dynamically build a multi-dimensional array in Java?

Questions: Suppose we have the Java code: Object arr = Array.newInstance(Array.class, 5); Would that run? As a further note, what if we were to try something like this: Object arr1 = Array.newInstance(Array.class, 2); Object arr2 = Array.newInstance(String.class, 4); Object arr3 = Array.newInstance(String.class, 4); Array.set(arr1, 0, arr2); Array.set(arr1, 1, arr3); Would arr1 then be a 2D… Read More »

What are the Advantages of Enhanced for loop and Iterator in Java?

Questions: can anyone please specify to me what are the advantages of Enhanced for loop and Iterators in java +5 ? Answers: The strengths and also the weaknesses are pretty well summarized in Stephen Colebourne (Joda-Time, JSR-310, etc) Enhanced for each loop iteration control proposal to extend it in Java 7: FEATURE SUMMARY: Extends the… Read More »

Java shutdown hook

Questions: I have added the following code to my program: Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() { @Override public void run() { System.out.println(“exit”); } }){}); I however do not see the message. Additional information: I am running the program from inside Netbeans IDE. EDIT: I forgot to add that there is a global Thread that keeps the program… Read More »

Java Date month difference

Questions: I have start date and end date. I need the number of months between this two dates in Java. For example From date: 2009-01-29 To date: 2009-02-02 It has one jan date and one Feb date. It should return 2. Answers: As the rest say, if there’s a library that will give you time… Read More »

Firebase with java (non-android) retrieve information

Questions: I have been trying to get my data for firebase database using java code(non-android). I used same method how I retrieved in android app. But its not getting data. Firebase firebase = new Firebase(“https://——.firebaseIO.com”); firebase.addValueEventListener(new ValueEventListener(){ @Override public void onDataChange(DataSnapshot ds) { long i = ds.child(“Users”).getChildrenCount(); userlist = new String[(int)i]; int j = 0;… Read More »

Resizing icon to fit on JButton in Java?

Questions: Whenever I set an icon for my JButton it is always not sized correctly. How can I resize the icon to fit the button fully? final JButton btnSanic = new JButton(); Image img = icon.getImage(); Image newimg = img.getScaledInstance(width, height, java.awt.Image.SCALE_SMOOTH); icon = new ImageIcon(newimg); btnSanic.setIcon(icon); Answers: There are any number of issues. To… Read More »