Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

simple date conversion

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

Question by:theartha
  • 4
  • 2
  • 2
  • +1
LVL 86

Expert Comment

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

Open in new window


Author Comment

ID: 35143370

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

Accepted Solution

CEHJ earned 500 total points
ID: 35143384
Well you could do a substring then ;)
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

LVL 47

Expert Comment

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

Author Closing Comment

ID: 35143408
Thanks CEHJ....
LVL 86

Expert Comment

ID: 35143417
LVL 47

Expert Comment

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 :)

LVL 92

Expert Comment

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
LVL 86

Expert Comment

ID: 35143644
>>use of substring is error prone

In what way?

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
print map entry 34 72
Arrays.asList  VS  ArrayList 4 71
runtime exception 2 41
learn programming 8 34
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 …
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

839 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