2

I have a database date field which return as

resultSet.getDate("startDate");

I would like to change the format of this to dd-MMM-yy and preserve as date type which means I wouldn't like to convert it to String.

I have the following code snippet to change date format

private Date getDate(ResultSet resultSet,Date columnName)
SimpleDateFormat format1 = new SimpleDateFormat("dd-MMM-yy");
String date = 
        new SimpleDateFormat("dd-MMM-yy").format(columnName);

What is the best approach to convert String again back to date with same date format

dd-MMM-yy
Jacob
  • 13,463
  • 58
  • 188
  • 302

5 Answers5

2

To convert String back to Date use:

Date date = new SimpleDateFormat("dd-MMM-yy", Locale.US).parse("01-JAN-84");

One way to retain Date data without converting to String is to call:

long time = date.getTime();

You can then construct it back to Date using:

Date date = new Date(time);

And you can use the SimpleDateFormat to format it to String as you please, whenever needed. Of course this approach is no different from passing the Date object directly.

Vladimír Schäfer
  • 14,559
  • 2
  • 45
  • 65
2

I would like to change the format of this to dd-MMM-yy and preserve as date type which means I wouldn't like to convert it to String.

That is not possible; you have a misunderstanding about what a Date object represents.

A Date object just contains a timestamp value, and it does not know anything about how it should be formatted when printed. So, you cannot have a Date object with a certain format.

Instead, you set the format on the SimpleDateFormat object, which you then use to convert your Date object to text.

Jesper
  • 186,095
  • 42
  • 296
  • 332
  • Jesper, I agree with you about what you have mentioned about Date as it may contain timestamp as well. As far this application is concerned there are no timestamp aspect for date. As I am receiving this from database as date, I would like to change the format and preserve data type as date. Thus I am looking for a solution on how to achieve this. – Jacob Apr 14 '14 at 11:08
  • 1
    @Polppan `java.util.Date` does not may contain a timestamp, it IS a timestamp. And about `java.sql.Date`: While semantically representing a date, it is not for use outside of jdbc context. And both types have no format information associated with. – Meno Hochschild Apr 14 '14 at 12:06
  • "I would like to change the format and preserve data type as date" - as I explained, that is not possible; you have the wrong idea of what a `Date` format is. A `Date` format does not have a format. You cannot have a `Date` object which has a certain format, or change the format of a `Date` object. – Jesper Apr 14 '14 at 19:08
  • Thanks Jesper. You were right from the beginning that it is impossible to convert Date object and preserve object as date. Appreciated. – Jacob Apr 21 '14 at 12:19
1
SimpleDateFormat format1 = new SimpleDateFormat("dd-MMM-yy");
String date = format1.format(columnName);
System.out.println(date);
// back to Date
Date date2 = format1.parse(date);
System.out.println(date2.toString());

You really need to use date formatting if you want to customize its output.

bhadz
  • 1,103
  • 2
  • 11
  • 21
  • Issue with this approach is format changes, for instance format changes back to `Thu Jun 06 00:00:00 GMT 2013` – Jacob Apr 14 '14 at 11:36
  • yes that's the way it is. you may create a helper class that will format the date whenever you pass a date to it. and you can access it anywhere in your application. – bhadz Apr 14 '14 at 14:48
1

you are in same boat as i was, i guess

convert datestring from one format to another in java simpledate format

after lot of analysis, i gave up converting the string back to date, since this will result in date format with specific to your system, to preserve the date format, you should always hold it to string type, i did converted my date type to string type for my date property.

i had this scenario,

i want to get interval between two dates, let us say i have db date format as "dd-MMM-yy" , but your system format is be "mm-dd-yyyy", so to compare two date, you need to convert you db format/parse to system format,it is supposed to be in same format to compare and it make sense to me

Community
  • 1
  • 1
pappu_kutty
  • 2,101
  • 6
  • 33
  • 71
  • Why I would like to have in Date format is because I am returning from database as date, thus it would be better to keep it same format for all future process with date. – Jacob Apr 14 '14 at 11:05
  • string type will preserve you date format, when you do simpledateformat.parse(date) will give date in system format. you can try this Locale.setDefault(Locale.US) for custom locale. – pappu_kutty Apr 14 '14 at 11:14
  • @Polppan try converting system default time format to your db format. this will be one way. – pappu_kutty Apr 14 '14 at 11:44
0

I suggest you to make the format reusable

private static SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yy");

and then do

Date converted = sdf.parse(dateString);