0

I have a date in the following format

//input date

Thu Jun 06 2013 00:00:00 GMT+0530 (India Standard Time)

//output date format

I want to change this to "dd-mm-yyyy, hh:mm:ss".

I get the input date format from db. I have to change that into output date format which i will be showing it in a grid.

I tried the following code.

DateFormat outputDate = new SimpleDateFormat("dd-mm-yyyy, hh:mm:ss"); 
                try
                {
                    Date date = outputDate.parse(facade.getDate.toString()); **//getting exception here**
                    outputDate = new SimpleDateFormat("dd-mm-yyyy, hh:mm:ss");
                    Date date1 = new SimpleDateFormat("dd-mm-yyyy, hh:mm:ss").parse(outputDate
                            .format(date));
                    facade.setDate(date1);
                }catch (ParseException e)
                {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

I am getting

java.text.ParseException: Unparseable date: "2013-06-06 00:00:00.0".

Any help..

sahana
  • 601
  • 5
  • 12
  • 33
  • what line are you getting the error at? – Bulbasaur Jun 07 '13 at 06:16
  • In this line Date date = outputDate.parse(facade.getDate.toString()); – sahana Jun 07 '13 at 06:17
  • For one thing, you want `dd-MM-yyyy, HH:mm:ss" as the output format. Read the docs for SimpleDateFormat. But you've got the same format everywhere - you're never specifying your *input* format. It's not entirely clear exactly what's in your input string. Does it actually contain the text "(India Standard Time)"? If so, you'll quite possibly need to remove that first. – Jon Skeet Jun 07 '13 at 06:18
  • yeah it contains the text India Standard Time. I am not sure about the input text format. so directly tried with the value. – sahana Jun 07 '13 at 06:19
  • 1
    Oh, and you need to consider which time zone you want to output the result in, too. – Jon Skeet Jun 07 '13 at 06:19
  • What kind of object is `facade.getDate`? – Sky Jun 07 '13 at 06:20
  • see here http://stackoverflow.com/questions/4203718/converting-string-to-date-with-timezone it may be helpful to u – PSR Jun 07 '13 at 06:20
  • 2
    It sounds like you're currently just trying any number of things without really thinking it through logically. Take a step back, and start again. Read the docs for SimpleDateFormat. Work out your input format and output format. – Jon Skeet Jun 07 '13 at 06:21
  • Date object in Thu Jun 06 2013 00:00:00 GMT+0530 (India Standard Time) format. – sahana Jun 07 '13 at 06:21
  • If it is a `Date` object I don't see a reason to parse it - or maybe I missed something... :-) – Sky Jun 07 '13 at 06:22
  • What is facade? Is it already a date? If so, you don't need to parse it. If its an object, see what format it is returning the date in? Its not matching your formatter.. – Bulbasaur Jun 07 '13 at 06:23
  • I don't know what the input format is. Could someone help me to find it out ? I checked http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html. None of the time formats in the table seem to be matching with the one I am getting. – sahana Jun 07 '13 at 06:24
  • the facade.getDate returns a date only. I just want to format it to dd-mm-yyyy, hh:mm:ss – sahana Jun 07 '13 at 06:25
  • So if `facade.getDate` is already a `Date`, the line `Date date = outputDate.parse(facade.getDate.toString())` is not necessary. You convert the date to a `String` just in order to parse it to a date again. Just use `facade.getDate` as input for `outputDate.format()`. – Sky Jun 07 '13 at 06:31

5 Answers5

1

"2013-06-06 00:00:00.0" does not match "dd-mm-yyyy, hh:mm:ss" your format should be "dd-MM-yyyy hh:mm:ss" instead

But, looking at your code I'm guessing facade.getDate is actually a java.sql.Timestamp which inherits from java.util.Date so you can directly pass it to the format like so

new SimpleDateFormat("dd-MM-yyyy, hh:mm:ss").format(facade.getDate)
shyam
  • 9,134
  • 4
  • 29
  • 44
1

Here's some code which works for me:

import java.text.*;
import java.util.*;

public class Test {
    public static void main(String[] args) throws Exception {
        String input = "Thu Jun 06 2013 00:00:00 GMT+0530 (India Standard Time)";
        DateFormat inputFormat = new SimpleDateFormat("E MMM dd yyyy HH:mm:ss 'GMT'z",
                                                      Locale.ENGLISH);
        Date date = inputFormat.parse(input);

        DateFormat outputFormat = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss",
                                                       Locale.ENGLISH);
        outputFormat.setTimeZone(TimeZone.getTimeZone("UTC"));

        String output = outputFormat.format(date);
        System.out.println(output);
    }
}

Things to consider:

  • You need to work out your output time zone. Currently I've got it set to UTC, but that may not be what you want.
  • You really need to take a step back and think things through. You've clearly got two different formats - you're trying to convert from one to the other. So creating three different SimpleDateFormat objects all with the same format is never going to work.
  • You need to read documentation carefully... in SimpleDateFormat, M means month and m means minute; h uses the 12-hour clock and H uses the 24-hour clock.

This is assuming you actually need to start with a string though. If getDate is already a Date or a Timestamp, you can ignore the first part - just use the output part of the above code. You should avoid unnecessary string conversions wherever possible.

Note that dd-MM-yyyy is a slightly unusual format - are you sure you don't actually want yyyy-MM-dd which is more common (and sortable)?

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • I want to convert the string again to date format !!! Could you please help ? – sahana Jun 07 '13 at 07:29
  • @sahana: Why do you need to do that? Try to avoid string conversions as far as you can. If you *really* need to do this though, just use the same `SimpleDateFormat` but call `parse` instead of `format`. – Jon Skeet Jun 07 '13 at 08:04
0
DateFormat outputDate = new SimpleDateFormat("yyyy-dd-mm hh:mm:ss");
try {
    Date date = outputDate.parse("2013-06-06 00:00:00.0");
    System.out.println(new SimpleDateFormat("dd-mm-yyyy, hh:mm:ss").format(date));
} catch (ParseException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

works well, line 1 was incorrect. Your SimpleDateFormat.parse needs to be in the exact format of the input date. Then you want to output it in a different format so you make another one and set the format then call SimpleDateFormat.format(date) and I put a println on it.

ug_
  • 11,267
  • 2
  • 35
  • 52
0

Fault is here

DateFormat outputDate = new SimpleDateFormat("dd-mm-yyyy, hh:mm:ss"); 

pattern should be equals to Thu Jun 06 2013 00:00:00 GMT+0530 (India Standard Time). not to your out put strings pattern.

Bishan
  • 15,211
  • 52
  • 164
  • 258
0
@Test
public void test() throws ParseException {
    SimpleDateFormat sdf_org = new SimpleDateFormat("E MMM dd yyyy HH:mm:ss 'GMT'Z", Locale.ENGLISH);
    Date d = sdf_org.parse("Thu Jun 06 2013 00:00:00 GMT+0530");
    SimpleDateFormat sdf_target = new SimpleDateFormat("yyyy-mm-dd HH:mm:ss.SSS");

    System.out.println(sdf_target.format(d));
}

output console : 2013-30-06 03:30:00.000

bistros
  • 1,139
  • 1
  • 9
  • 23