7 Examples of String.format() and printf() in Java - Formatting Text and String in Java

You can format String in Java either by using String.format() method or by using System.printf() method, both uses TextFormat class to format String input internally. Both of these methods are inspired from C-Style printf() method and formatting instruction are very similar to C e.g. %s for String, %d for numeric, %f for floating point value and %n for new lines etc, but it is not exactly same as C. Some customization have been made to accommodate some Java language features. Java's String formatting is also more strict than C's, for example, if a conversion is incompatible with a flag then Java throws an Exception ( java.util.IllegalFormatConversionException), while in C, incompatible flags are silently ignored.

How to convert Timestamp to Date in Java -JDBC Example Tutorial

In the last article, I have shown you how to convert Date to Timestamp in Java and today we'll learn about converting timestamp value from database to Date in Java. As you remember, the JDBC API uses separate Date, Time and Timestamp class to confirm DATE, TIME and DATETIME data type from the database, but most of the Java object oriented code is written in java.util.Date. This means you need to know how to convert the timestamp to date and vice-versa. You can do by using the getTime() method, which return the number of millisecond from Epoch value. This can be used to create both Timestamp and java.util.Date, hence it acts as a bridge between Date class from java.util package and Date, Time and Timestamp class from the java.sql package. Like Date, Timestamp also contains both date and time value, so you won't see empty or zero time we saw previously while converting SQL Date to java.util.Date.

Binary tree post order traversal in Java with example

In last couple of articles, we have learned about pre-order and in-order tree traversal in Java and today, you will learn about the post order traversal in binary tree. The post order traversal is also a depth-first algorithm because you go deep before you visit other nodes in same level. In post order traversal, you first visit left subtree, then right subtree and finally you print the value of node or root. That's why the value of root is always printed last on post order traversal. Like many tree algorithms, the easiest way to implement post-order traversal is by using recursion. In fact, if you know how to write pre-order using recursion, you can use the same algorithm with bit of adjustment to implement post order traversal. All you need to do is instead of printing the value of node first, just call the recursive method with left subtree as shown in our example.