Solved

simple date conversion

Posted on 2011-03-15
9
306 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
 
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
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.

 

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

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
groupSum6 challenge 6 77
@SBGen Method 3 38
eclipse compiler vs Installed JREs option 3 77
Error trying to install RTMT Win7 5 41
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…

862 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

24 Experts available now in Live!

Get 1:1 Help Now