• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Convert String to Date

 
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79177
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 138
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic