Solved

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

Posted on 2009-05-08
16
1,242 Views
Last Modified: 2012-05-06
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
Comment
Question by:VikasGupta79
  • 7
  • 7
  • 2
16 Comments
 
LVL 92

Expert Comment

by:objects
ID: 24334268
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
 

Author Comment

by:VikasGupta79
ID: 24334280
But I need Java.util.Date object to be returned.... if I return String than how will I get date object..
0
 
LVL 92

Expert Comment

by:objects
ID: 24334290
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
 
LVL 12

Expert Comment

by:Gibu George
ID: 24334299
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
 

Author Comment

by:VikasGupta79
ID: 24334320
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
 
LVL 12

Expert Comment

by:Gibu George
ID: 24334373
Sorry!! my answer was wrong. I would like to know how you are checking the format of the date after conversion
0
 
LVL 12

Expert Comment

by:Gibu George
ID: 24334384
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
 

Author Comment

by:VikasGupta79
ID: 24334434
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 12

Expert Comment

by:Gibu George
ID: 24334512
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
 

Author Comment

by:VikasGupta79
ID: 24334808
It does not solve my problem, so is there any way I get the formatted date object
0
 
LVL 12

Expert Comment

by:Gibu George
ID: 24334852
Why you want a formatted date object?
0
 

Author Comment

by:VikasGupta79
ID: 24334882
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
 
LVL 12

Expert Comment

by:Gibu George
ID: 24334994
Does the sorting api expects the date in MM/dd/yyyy HH:mm:ss format or will it take any format?
0
 

Author Comment

by:VikasGupta79
ID: 24335009
our application requirement is MM/dd/yyyy HH:mm:ss  format....so we cant devate from that
0
 
LVL 12

Accepted Solution

by:
Gibu George earned 50 total points
ID: 24335027
You have control over the presentation part, so format it while displaying only.
0
 

Author Closing Comment

by:VikasGupta79
ID: 31579357
Thanks allot for your help!!!!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Apache-Tomcat Single Sign On with NTLM/Kerberos on Windows 2008R2 3 199
attribute vs parameter and setter vs add method 17 83
console vs log file 16 77
spring example errors 33 250
With more and more people tethered to the internet, advertisers can reach you in new and creative ways. Push notifications on your smartphone, customized advertisements based on your previous searches, and email inboxes filled with promotional pitch…
Note: This is the second blog post in a series on email clearinghouses (https://www.xmatters.com/alert-management/blog-email-has-failed-us?utm_campaign=70138000000ydLoAAI&utm_source=exex&utm_medium=article&utm_content=blog-post).   Every month t…
Delivering innovative fully-managed cloud services for mission-critical applications requires expertise in multiple areas plus vision and commitment. Meet a few of the people behind the quality services of Concerto.
Need to grow your business through quality cloud solutions? With everything required to build a cloud platform and solution, you may feel like the distance between you and the cloud is quite long. Help is here. Spend some time learning about the Con…

929 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now