Java: Converting String to Date

I am trying to get the current date in a Talend job and I am using this as my context variable:

DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date date = new Date();
context.dateout = dateFormat.format(date);

System.out.println(context.dateout);

However, the type of the result is a String and not a Date. How should I correct it?

Thank you very much!!

Answers


Try to do that according the following code:

String string = "2016-03-15";
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
try {
    Date date = dateFormat.parse(string);
    System.out.println(date);
} catch (ParseException ex) {
    System.out.println(ex);
}

I don't know what your context.dateout means.


Note the difference between parse and format.

This is to create a string from a date:

dateFormat.format(date);

This is to create a date from a string:

dateFormat.parse(dateString);

Need Your Help

Return with a 500 error from inside a JSP include

java jsp jspinclude

Is there a way to, from inside of a jsp:include page, get its requesting page to respond with an HTTP 500 error? I've tried using the response.sendError(418, "I'm a teapot.");, but that only works ...

Windsor - pulling Transient objects from the container

.net dependency-injection castle-windsor ioc-container

How can I pull objects from the container that are transient in nature? Do I have to register them with the container and inject in the constructor of the needing class? Injecting everything into the