Results 1 to 5 of 5

Thread: Manipulating a Date in java

  1. #1
    Join Date
    Nov 2009
    Posts
    333

    Manipulating a Date in java

    Hello,
    I have a class attribute of type date, I can retrieve the day, month and year to date I have only seen separately in the API methods that getMonth (), etc. are no longer be used. It should (according to API) use, Calendar.get (Calendar.DAY_OF_MONTH). Can you explain how?
    Here is my code
    Code:
    ...
    Date dt;
    ...
    Public String toString(){
        return ("Date:" + MyDate.getMonth());
    ...
    }
    I need a help in manipulating date in java. Any explanation will be appreciated.

  2. #2
    Join Date
    Nov 2009
    Posts
    583

    Re: Manipulating a Date in java

    Hello,
    Just have a look at the sample code, it is a simple code. It will be better for your understanding.
    Code:
    GregorianCalendar foe = new GregorianCalendar();
    foe.setTime( unObjetDate );
     
    int month = foe.get(GregorianCalendar.MONTH);
    int year = foe.get(GregorianCalendar.YEAR);
    int day = foe.get(GregorianCalendar.DAY_OF_MONTH);
    Hope this will help you.

  3. #3
    Join Date
    Nov 2009
    Posts
    333

    Re: Manipulating a Date in java

    Hello,
    I have another question, always on my dates. I get a date as "dd / MM / YYYY" (String), I must first convert the Date type (I must) and then display it separately. Here is my piece of code:
    Code:
    DateFormat dtf = new SimpleDateFormat("dd / MM / yyyy");
    Date d = dtf.parse("22/03/2008");
    ...
    GregorianCalendar geo = new GregorianCalendar();
    geo.setTime(d);
    int month = geo.get(GregorianCalendar.MONTH);
    int year = geo.get(GregorianCalendar.YEAR);
    int day = geo.get(GregorianCalendar.DAY_OF_MONTH);
    ...
    System.out.System.out.println("Day" + + Day "Month" Month + + "Year" + Year);
    The display of the day and year is correct but the display of the month is always smaller than 1! In this case, the displayed month is 02 ... Do you know where is the problem? Thanks in advance.

  4. #4
    Join Date
    Nov 2009
    Posts
    518

    Re: Manipulating a Date in java

    Hello,
    Just have a look at the following code.
    Code:
    public class Example
    {
    public static final String dt = "dd-MM-yyyy HH:mm:ss";
    
    // public static String now()
    //{
    // Calendar c = Calendar.getInstance();
    // SimpleDateFormat s = new SimpleDateFormat(dt);
    // return s.format(c.getTime());
    //}
    public static void main(String[] args)
    
    {
    //Step1:Getting Time Zone "country"
    Calendar c = Calendar.getInstance();
    String tmzn = c.getTimeZone().getDisplayName();
    System.out.println("tmzn is : " + tmzn );
    
    //Step2:Getting Time in Milli Seconds
    System.out.println("Current Time in MiliSeconds : " + c.getTimeInMillis() );
    
    //Step3:Getting current time in date format
    
    //Calendar c = Calendar.getInstance();
    SimpleDateFormat s = new SimpleDateFormat(dt);
    
    String time=s.format(c.getTime());
    System.out.println(time);
    // System.out.println("Now : " + Example.now());
    }
    
    }

  5. #5
    Join Date
    Nov 2009
    Posts
    583

    Re: Manipulating a Date in java

    Hello,
    This is another sample code for you.
    Code:
    import java.text.SimpleDateFormat;
    import java.util.Date;
    public class DateExample2 {
     
        public static void main(String[] args) {
     
            SimpleDateFormat simdt =
                new SimpleDateFormat("EEEE-MMMM-dd-yyyy");
     
            Date dt = new Date();
     
            System.out.println(simdt.format(dt));
        }
    }

Similar Threads

  1. Getting Current Date in Java
    By Level8 in forum Software Development
    Replies: 5
    Last Post: 27-02-2010, 05:26 PM
  2. Exactly match date in java
    By Vodka in forum Software Development
    Replies: 5
    Last Post: 13-02-2010, 02:00 AM
  3. Query with Date in java
    By Gunner 1 in forum Software Development
    Replies: 5
    Last Post: 12-02-2010, 03:55 AM
  4. Find date in java
    By Allan.d in forum Software Development
    Replies: 3
    Last Post: 09-12-2009, 10:43 AM
  5. How to do the Date comparison in Java
    By Rock Villa in forum Windows Software
    Replies: 3
    Last Post: 06-08-2009, 09:44 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,750,826,734.56644 seconds with 16 queries