Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1270
  • Last Modified:

Java String date conversion from one format to another Java Date format

I need to convert the Java date from one format to another format and get returned date in new format which shoud be java.util.Date object only.

I have written following given below method but I am getting same output as input.

calling of method
convertStringToDateNew("Wed Apr 29 01:12:44 IST 2009", "MM/dd/yyyy HH:mm:ss");
public static Date convertDateToNewFormat(String sDate, String dateFormat)  throws TechnicalException
    {
        SimpleDateFormat expectedDateFormat = new SimpleDateFormat("E MMM dd HH:mm:ss z yyyy");
        SimpleDateFormat requiredDateFormat= new SimpleDateFormat(dateFormat);
       
        Date date = null;
        try
        {
        	if(sDate != null)
        	{
	            date = expectedDateFormat.parse(sDate);
	            String strDate = requiredDateFormat.format(date);
	            System.out.println("String date ="+strDate);
	            date = requiredDateFormat.parse(strDate);
 
           }
        } catch (Exception e)
        {
        	throw new TechnicalException ("Exception in [CommonUtil][convertStringToDate]", e.getMessage());			
        }
        return date;		 
    }

Open in new window

0
VikasGupta79
Asked:
VikasGupta79
  • 7
  • 7
  • 2
1 Solution
 
objectsCommented:
your method should return astring, not a Date. Date has no idea about formatting

public static String convertDateToNewFormat(String sDate, String dateFormat)  throws TechnicalException
    {
        SimpleDateFormat expectedDateFormat = new SimpleDateFormat("E MMM dd HH:mm:ss z yyyy");
        SimpleDateFormat requiredDateFormat= new SimpleDateFormat(dateFormat);
       
        String strDate = null;
        try
        {
                if(sDate != null)
                {
                    Date date = expectedDateFormat.parse(sDate);
                   strDate = requiredDateFormat.format(date);
                    System.out.println("String date ="+strDate);
           }
        } catch (Exception e)
        {
                throw new TechnicalException ("Exception in [CommonUtil][convertStringToDate]", e.getMessage());                        
        }
        return strDate;            
    }
0
 
VikasGupta79Author Commented:
But I need Java.util.Date object to be returned.... if I return String than how will I get date object..
0
 
objectsCommented:
if you need the date object then you have to handle formatting it elsewhere
the method you have effectively does nothing, it just returns the same date that you pass it

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Gibu GeorgeCTOCommented:
Try this
public static Date convertDateToNewFormat(String sDate, String dateFormat)  throws TechnicalException
    {
        SimpleDateFormat requiredDateFormat= new SimpleDateFormat(dateFormat);
       
        Date date = null;
        try
        {
              if(sDate != null)
              {
                  date = requiredDateFormat.parse(sDate);
 
           }
        } catch (Exception e)
        {
              throw new TechnicalException ("Exception in [CommonUtil][convertStringToDate]", e.getMessage());                  
        }
        return date;             
    }

Open in new window

0
 
VikasGupta79Author Commented:
Ok..how to do that, I mean I have date object which has date in "Wed Apr 29 01:12:44 IST 2009" format now I need to format this date object into "MM/dd/yyyy HH:mm:ss" date format...
0
 
Gibu GeorgeCTOCommented:
Sorry!! my answer was wrong. I would like to know how you are checking the format of the date after conversion
0
 
Gibu GeorgeCTOCommented:
Your code is correct and is working fine, just do an SOP of the returned date and it prints out with the format which you passed.
0
 
VikasGupta79Author Commented:
SOP will work fine becox its internally calls toString() method ....I have to set the new formatted date string in date object as its required for me to set this formatted date in another method....
0
 
Gibu GeorgeCTOCommented:
In date object date is kept as time in millis not in any particular format. You can format it for display purposes as string using SimpleDateFormat
0
 
VikasGupta79Author Commented:
It does not solve my problem, so is there any way I get the formatted date object
0
 
Gibu GeorgeCTOCommented:
Why you want a formatted date object?
0
 
VikasGupta79Author Commented:
becx
1. I cant shw "Wed Apr 29 01:12:44 IST 2009" this kind of date to user as we have different format on UI
2. I need date object only as I need sort the list of items on date object.

I dont have any control on sorting API's as its framework provided.....so I got stuck here...
0
 
Gibu GeorgeCTOCommented:
Does the sorting api expects the date in MM/dd/yyyy HH:mm:ss format or will it take any format?
0
 
VikasGupta79Author Commented:
our application requirement is MM/dd/yyyy HH:mm:ss  format....so we cant devate from that
0
 
Gibu GeorgeCTOCommented:
You have control over the presentation part, so format it while displaying only.
0
 
VikasGupta79Author Commented:
Thanks allot for your help!!!!
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 7
  • 7
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now