Solved

simple date conversion

Posted on 2011-03-15
9
305 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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

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…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

743 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

12 Experts available now in Live!

Get 1:1 Help Now