-1

I have a Date() object which always gives me date in a particular format (Comes in-built with Java). I want to get store Date in a specified format ("MMMM, dd yyyy HH:mm:ss"). I cam across one similar approach to do that-

Date date = new Date();
String dateFormat = "MMMM, dd yyyy HH:mm:ss";
SimpleDateFormat dt1 = new SimpleDateFormat(dateFormat);
dt1.format(date);

dt1.format(date) returns a formatted date in String data type. I want something similar which will return formatted date in Date data type.

Pavlo
  • 43,301
  • 14
  • 77
  • 113
Sitam Jana
  • 3,123
  • 2
  • 21
  • 40

2 Answers2

3

I want something similar which will return formatted date in Date data type.

That is not possible. Date objects do not have a format by themselves, you cannot have a Date object in a specific format. You'll have to find another way to do what you need to do.

Jesper
  • 202,709
  • 46
  • 318
  • 350
  • Answer is correct. A java.util.Date represents a moment on the time line of the Universe, implemented as the number of milliseconds since the beginning of 1970 (UTC/GMT). A single Date instance may be used to generate strings in various human-readable formats for different countries & cultures for various time zones around the world. But the Date instance is never "in a format", the Date instance is used to *generate a string* that is "in a format". Same goes for a DateTime instance in the alternative [Joda-Time](http://www.joda.org/joda-time/) library. – Basil Bourque Jan 25 '14 at 05:21
1

Your question is very unclear but I suspect what you want is parse a given String with a date into a Date object? If that is so, this should help: Java string to date conversion.

Given your example code, it would look like this:

String dateString = "June, 01 2014 08:23:51";
String dateFormat = "MMMM, dd yyyy HH:mm:ss";
Date date = new SimpleDateFormat(dateFormat, Locale.ENGLISH).parse(dateString);
Community
  • 1
  • 1
Fabian Ritzmann
  • 1,345
  • 9
  • 20