Results 1 to 8 of 8
  1. #1
    judy318 is offline Member
    Join Date
    Oct 2009
    Location
    Texas
    Posts
    7
    Rep Power
    0

    Question julian date to full date format

    without using util.calendar, say i have int 156 , i need to change this to full date format. can some one give me a hint?

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    You'll have to tell us your rules first. How is 156 a full date format?

  3. #3
    judy318 is offline Member
    Join Date
    Oct 2009
    Location
    Texas
    Posts
    7
    Rep Power
    0

    Default 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 06:39 AM.

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    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)

  5. #5
    CodesAway's Avatar
    CodesAway is offline Senior Member
    Join Date
    Sep 2009
    Location
    Texas
    Posts
    238
    Rep Power
    5

    Default

    Java 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

  6. #6
    judy318 is offline Member
    Join Date
    Oct 2009
    Location
    Texas
    Posts
    7
    Rep Power
    0

    Default

    wow! cool! i still wants to try it with out import java.util.Calendar; but still help me a lot.
    thank you for reply!

  7. #7
    CodesAway's Avatar
    CodesAway is offline Senior Member
    Join Date
    Sep 2009
    Location
    Texas
    Posts
    238
    Rep Power
    5

    Default

    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

  8. #8
    judy318 is offline Member
    Join Date
    Oct 2009
    Location
    Texas
    Posts
    7
    Rep Power
    0

    Talking completed

    I had to do it long way...but got it is done and it is working! uhoo!:p

Similar Threads

  1. How to change the format of date?
    By jboy in forum New To Java
    Replies: 1
    Last Post: 09-09-2009, 01:22 PM
  2. Date Format
    By learnspring in forum New To Java
    Replies: 1
    Last Post: 11-16-2008, 05:16 PM
  3. How to format the date in particular pattern
    By Java Tip in forum java.text
    Replies: 0
    Last Post: 04-04-2008, 02:35 PM
  4. How to format the date in particular pattern
    By JavaBean in forum Java Tip
    Replies: 0
    Last Post: 10-04-2007, 09:28 PM
  5. problems with Date format
    By tommy in forum New To Java
    Replies: 1
    Last Post: 07-25-2007, 08:38 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •