Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Java in General and the fly likes Convert String to Date Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Convert String to Date" Watch "Convert String to Date" New topic
Author

Convert String to Date

naga eswar
Ranch Hand

Joined: Jan 25, 2012
Posts: 120
hi all.... The issue is regaring date to convert String into Date.

Below is the code i used for conversion...




Out put is New Date: Mon Jul 16 13:45:12 IST 2012

But i need date format to be dd-MM-yyyy with time stamp and that date should of Date datatype.
Cab any body help me please.
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
formatter.parse() already returns an object of type java.util.Date and hence you don't need to cast again. You can use an another DateFormat object with desired format to formed the new date (like formatter you already have).

Note that format() method will return only String representation and you already have a Date object created using the parse() method.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40027
    
  28
I am not sure I understand the question. Are you trying to get a date out of a String, or display that resultant date as a String?
This is what happened when I copied your code
[campbell@xxx java]$ javac DateEx.java
Note: DateEx.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
[campbell@campbell-inspiron1110 rubbish]$ java DateEx
New Date: Sat Mar 01 00:00:00 GMT 3913
You ought not to use new Date(int, int, int).
You need to check how you are setting your date, and why I am getting 1sr March 3913 from exactly the same code you posted.
Marc Heruela
Ranch Hand

Joined: Jul 23, 2009
Posts: 38
Try this:





note that just printing newDate (java.util.Date type) will just call the toString() method. If you need specific formatting, use SimpleDateFormatter.format()


-marckun
Marc Heruela
Ranch Hand

Joined: Jul 23, 2009
Posts: 38
Also if you want to generate java.util.Date out from a Calendar object, i think Calendar.getTime() is a much better approach.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40027
    
  28
You ought not to use number literals for dates; use Calendar.JANUARY, etc.
Marc Heruela
Ranch Hand

Joined: Jul 23, 2009
Posts: 38
Campbell Ritchie wrote:You ought not to use number literals for dates; use Calendar.JANUARY, etc.

haha.. good one..
naga eswar
Ranch Hand

Joined: Jan 25, 2012
Posts: 120
OMG ... Really Really thanks for you all....

Really its working if we use Calendar.

The problem in Date is that some methods are deprecated. Finally i got the solutions by your suggestions.

Here the main thing that, we are fetching two dates (lets say dtInstr and newDate) from back-end(data base). Based on dtInstr we have to genarate newDate.
This new from back end will be 15 days after dtInstr. This calculation made at backend only.

At front end we have to handle as below

if day of dtInstr =16 then newDate = 01/ next month of month dtInstr / year
else if day of dtInstr =23 then newDate = 08/ next month of month dtInstr / year
else newDate = newDate from back end

in 16 and 23 dates, we have to hard code the new date as string and convert into Date because we already have DVO which contains getters and setters method of date.

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40027
    
  28
Marc Heruela wrote: . . . haha.. good one..
There is nothing new about my suggestion; it is a standard principle to avoid number literals.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14420
    
  23

Campbell Ritchie wrote:
Marc Heruela wrote: . . . haha.. good one..
There is nothing new about my suggestion; it is a standard principle to avoid number literals.

Especially since the month numbers to be used with java.util.Calendar are 0-based instead of 1-based, which is very confusing.

So, January = 0, February = 1, ..., July = 6, ... December = 11 instead of the normal numbers 1 - 12 that we are used to.

If you use Calendar.JULY instead of the plain numbers, it's much clearer which month you mean.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Convert String to Date