Java 8 interview questions - vidyasekaran/current_learning GitHub Wiki

Date conversion in java

https://www.quora.com/What-is-the-difference-between-format-and-parse-in-Java-date

You probably mean dateformat.parse() and dateformat.format().

dateformat.format() formats a java date object to a user readable string representation.

If you have the string representation, you can use dateformat.parse() to get the Date object.

See following code for example

import java.text.SimpleDateFormat;
import java.text.DateFormat; import java.util.Date;
public class StringToDateExample1 {
public static void main(String[] args)throws Exception {
final Date originalDate = new Date(System.currentTimeMillis());
final DateFormat df = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss");

//java date object to a user readable string representation String stringFormattedDate = df.format(originalDate);

//If you have the string representation, you can use dateformat.parse() to get the Date object. Date parsedDateFromStringFormat = df.parse(stringFormattedDate);

System.out.println(stringFormattedDate);

}