Jacob Nelson
Jacob Nelson

Reputation: 3006

Date format in Java

Few questions:

  1. How can I print now (The current Date/Time) in the format below.
  2. How can I convert a string date, into a date object if I know the format, same format as below.

Example: 2011-05-04 19:12:46 -0500

Format: YYYY-MM-DD HH:MM:SS [+|-]hh[mm]

Upvotes: 1

Views: 635

Answers (2)

Mike Tunnicliffe
Mike Tunnicliffe

Reputation: 10762

Check out SimpleDateFormat: http://download.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html

SimpleDateFormat.format() and SimpleDateFormat.parse()

Upvotes: 3

Try:

Date yourDate = new Date(...);
SimpleDateFormat myDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss Z");
System.out.print(myDateFormat.format(yourDate);

The link to SimpleDateFormat Javadoc is here.

Upvotes: 6

Related Questions