?
Solved

simple date conversion

Posted on 2011-03-15
9
Medium Priority
?
315 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 2000 total points
ID: 35143384
Well you could do a substring then ;)
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
In this post we will learn different types of Android Layout and some basics of an Android App.
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 will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses
Course of the Month17 days, 3 hours left to enroll

864 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