Results 1 to 8 of 8
Thread: julian date to full date format
- 11-01-2009, 05:29 AM #1
Member
- Join Date
- Oct 2009
- Location
- Texas
- Posts
- 7
- Rep Power
- 0
- 11-01-2009, 07:09 AM #2
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 14
You'll have to tell us your rules first. How is 156 a full date format?
- 11-01-2009, 07:35 AM #3
Member
- Join Date
- Oct 2009
- Location
- Texas
- Posts
- 7
- Rep Power
- 0
julian date
julian date 156, like say julian date 30 is January 30th...etc when i have 156 julian date, I need to change to thursday May 25th somthing like that. I am trying with enum type, weekdays, month, days and also trying it with switch... i'll figure it out eventually but wondering, which way would be the best way...
Last edited by judy318; 11-01-2009 at 07:39 AM.
- 11-01-2009, 08:07 AM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,716
- Rep Power
- 19
Calendar has a method that lets you set any aspect of the date: including the day of the year.
After that you can obtain the Date and do whatever you want with it. If you are after a String form, use a SimpleDateFormat. (or you can use printf() with the Calendar directly - see the example "Duke's Birthday" in the Formatter documentation)
(It's June 5 this year)
- 11-01-2009, 08:21 AM #5Java Code:
import java.util.Calendar; public class Test { /** * Main method * * @param args * (not used) */ public static void main(String[] args) { // March 1st (for non-leap years) // February 29th (for leap years) int julianDate = 60; Calendar calendar = Calendar.getInstance(); // uses current year int year = calendar.get(Calendar.YEAR); // year = 2008; calendar.set(Calendar.YEAR, year); calendar.set(Calendar.MONTH, Calendar.JANUARY); // since calendar is lenient, by setting the month to January (0), // the date can be set to the julian date, and it will automatically be // set to the correct date // System.out.println(calendar.isLenient()); calendar.set(Calendar.DATE, julianDate); // format as desired // e.g. March 1, 2009 System.out.printf("%1$tB %1$te, %1$tY", calendar); } }
CodesAway - codesaway.info
writing tools that make writing code a little easier
- 11-01-2009, 10:14 AM #6
Member
- Join Date
- Oct 2009
- Location
- Texas
- Posts
- 7
- Rep Power
- 0
wow! cool! i still wants to try it with out import java.util.Calendar; but still help me a lot.
thank you for reply!
- 11-02-2009, 12:41 AM #7
What's wrong with importing util.Calendar? - it's part of the standard lib, so it's not like your program gets any bigger by using it.
Actually, by creating your own method to do it, your programs would rely on an external library AND be bigger for doing so.
However, if you want, it's not a hard method to write. Here's the skeleton of it.
1) Write a MyDate class that stores a month, day, and year
2) Have a method that creates a MyDate object for the given Julian date
The method would:
1) Initialize that date with January <julianDate> (of some year)
2) While the day is greater than the number of days in that month (accounting for leap year, for February), decrease the day by the total number of days in that month, and then increase the month by one.
(This would be easiest done by a function that takes the Month/Year and returns the number of days in that month, in the given year)
3) After the loop is done, your date will be a valid date from the given Julian date.
Note that a similar method can be used to increase/decrease the date by a given number of days.CodesAway - codesaway.info
writing tools that make writing code a little easier
- 11-02-2009, 01:17 PM #8
Member
- Join Date
- Oct 2009
- Location
- Texas
- Posts
- 7
- Rep Power
- 0
Similar Threads
-
How to change the format of date?
By jboy in forum New To JavaReplies: 1Last Post: 09-09-2009, 02:22 PM -
Date Format
By learnspring in forum New To JavaReplies: 1Last Post: 11-16-2008, 06:16 PM -
How to format the date in particular pattern
By Java Tip in forum java.textReplies: 0Last Post: 04-04-2008, 03:35 PM -
How to format the date in particular pattern
By JavaBean in forum Java TipReplies: 0Last Post: 10-04-2007, 10:28 PM -
problems with Date format
By tommy in forum New To JavaReplies: 1Last Post: 07-25-2007, 09:38 PM
Bookmarks