Solved

simple date conversion

Posted on 2011-03-15
9
309 Views
Last Modified: 2012-05-11
Hi There,

How to convert yyyy-mm-dd hh:mm:ss.sss to only yyyy as a String.

i.e: my value in DB of type datetime, 2009-11-04 00:00:00.0 to 2009 as a String.

Thanks.
0
Comment
Question by:theartha
  • 4
  • 2
  • 2
  • +1
9 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 35143357
Simplest would be
String d = String.format("%tY", resultSet.getDate(someCol));

Open in new window

0
 

Author Comment

by:theartha
ID: 35143370
@CEHJ:

what if the date is not coming from resultSet, but just as String?
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 500 total points
ID: 35143384
Well you could do a substring then ;)
0
Industry Leaders: 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!

 
LVL 47

Expert Comment

by:for_yan
ID: 35143385

I do it this way:

select to_char(date_col_name,'YYYY') from table

  String dateString = resultSet.getString(1)

Works with Orcale, don't know if it works with other databases
0
 

Author Closing Comment

by:theartha
ID: 35143408
Thanks CEHJ....
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35143417
:)
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35143419


If it comes just as String, you can use SimpleDateFormat

SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
SimpleDateFormat f1 = new SimpleDateFormat("yyyy");

java.util.Date dd = f.parse(dateString, new ParsePosition(0));

String newString = f1.format(dd);

Substring is of course simpler :)




 
0
 
LVL 92

Expert Comment

by:objects
ID: 35143632
use of substring is error prone, best to use SimpleDateFormat as for_yan suggested above to parse the date string, then format it as a string as required
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35143644
>>use of substring is error prone

In what way?
0

Featured Post

Independent Software Vendors: 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!

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

713 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