Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Convert String to Date

 
naga eswar
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 48967
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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()
 
Marc Heruela
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 48967
60
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You ought not to use number literals for dates; use Calendar.JANUARY, etc.
 
Marc Heruela
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:You ought not to use number literals for dates; use Calendar.JANUARY, etc.

haha.. good one..
 
naga eswar
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 48967
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15284
39
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic