Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / July 2008

Tip: Looking for answers? Try searching our database.

Question about dates in Java

Thread view: 
I_Am_The_Walrus - 05 Jul 2008 02:53 GMT
If I have a string which represents the year and day of the year
i.e. (2005001) is there an easy way to determine the date
i.e. (January 1, 2005).   I have done some googling and looked at the API,
but nothing leaps out at me.  Even looking atRoedy's site isn't helping
much.  I could probably write a conversion routine, but with Java's rich
API, I thought there might be a way to do this without reinventing the
wheel.

Thanks
Lew - 05 Jul 2008 03:04 GMT
> If I have a string which represents the year and day of the year
> i.e. (2005001) is there an easy way to determine the date
[quoted text clipped - 3 lines]
> API, I thought there might be a way to do this without reinventing the
> wheel.

You could use
<http://java.sun.com/javase/6/docs/api/java/util/Calendar.html#set(int,%20int)>

Use input.substring( 4, 7 ) to extract the Julian day.  Use substring( 0, 4 )
to get the year.  Set the year with

 set( Calendar.YEAR, year );

and the day of the year with

 set( Calendar.DAY_OF_YEAR, julianDay );

You should consider making the API Javadocs a primary source.

Signature

Lew

Baba O'Reilly - 05 Jul 2008 15:55 GMT
>> If I have a string which represents the year and day of the year
>> i.e. (2005001) is there an easy way to determine the date
[quoted text clipped - 18 lines]
>
> You should consider making the API Javadocs a primary source.

The thing is, I knew what I wanted to do and I knew there had to be
something in the API that could handle it.  But when you don't know
where to start looking, the API can be quite daunting.  I actually
did some google searches and found something similar that, when
I plugged into my code, was pretty darned close, but the dates I
was getting out were way off.  It was my SimpleDateFormat that was wrong.
Once I had it fixed, my program works like a charm.

Signature

I have nothing to add to this

Lew - 05 Jul 2008 03:05 GMT
> If I have a string which represents the year and day of the year
> i.e. (2005001) is there an easy way to determine the date
> i.e. (January 1, 2005).  

That should be "e.g.," (with a comma), not "i.e.".

Signature

Lew

Stefan Ram - 05 Jul 2008 03:28 GMT
>If I have a string which represents the year and day of the
>year i.e. (2005001)

 If the String would be

1234

 , is this day 234 of year 1, day 34 of year 12, or day 4 of
 year 123, or another day of another year, or not an element
 of the input notation?

 To write a parser and interpreter for a language, you need a
 specification for the

     - syntax and the
     - semantics of this language.

 The syntax is the set of all valid representations, the
 semantics gives the year and day of the year for each element
 of this set.

 One example or even several examples do not provide this.

 This newsgroup comp.lang.java.help is intended to give help
 for the programming language Java. So if you have specified
 your syntax and semantics, then you can get help on how to
 implement it in Java here.

 But if you do not provide syntax or semantics, one does not
 know, what it /is/ that you want to implement.

     »Hi, I have invented a new number system, that represents
     values in a more compact and convenient way
     i.e. (423@f4)

     Is there a way to read this with Java?«
Roedy Green - 05 Jul 2008 09:51 GMT
On 05 Jul 2008 01:53:30 GMT, "I_Am_The_Walrus"
<thewalrus@will_f_u_upINvalid> wrote, quoted or indirectly quoted
someone who said :

>i.e. (2005001)
see http://mindprod.com/jgloss/calendar.html#PARSING

All you have to do is change the SimpleDateFormat mask.
Signature


Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

Roland de Ruiter - 05 Jul 2008 15:20 GMT
> If I have a string which represents the year and day of the year
> i.e. (2005001) is there an easy way to determine the date
[quoted text clipped - 5 lines]
>
> Thanks
So 2005365 would represent December 31st of 2005?

Are there always 4 digits for the year and 3 digits for the day of the
year? Then you could use a SimpleDateFormat with a pattern containing
"D" for a "Day in year" field. For example:

import java.text.*;
public class YearAndDayOfYear {
   public static void main(String[] args) throws Exception {
      DateFormat dateParser = new SimpleDateFormat("yyyyDDD");
      System.out.println(dateParser.parse("2005001"));
      System.out.println(dateParser.parse("2005365"));
   }
}
Signature

Regards,

Roland



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.