Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Register Tags Active Topics RSS Search Mark Forums Read SiteMap

Tags: , , , , , ,

Sponsored Links



Manipulating a Date in java

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 07-02-2010
Member
 
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.
Reply With Quote
  #2  
Old 07-02-2010
Member
 
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.
Reply With Quote
  #3  
Old 07-02-2010
Member
 
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.
Reply With Quote
  #4  
Old 07-02-2010
Member
 
Join Date: Nov 2009
Posts: 520
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());
}

}
Reply With Quote
  #5  
Old 07-02-2010
Member
 
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));
    }
}
Reply With Quote
Reply

  TechArena Community > Software > Software Development


Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Manipulating a Date in java"
Thread Thread Starter Forum Replies Last Post
Getting Current Date in Java Level8 Software Development 5 27-02-2010 05:26 PM
Exactly match date in java Vodka Software Development 5 13-02-2010 02:00 AM
Query with Date in java Gunner 1 Software Development 5 12-02-2010 03:55 AM
Find date in java Allan.d Software Development 3 09-12-2009 10:43 AM
How to do the Date comparison in Java Rock Villa Windows Software 3 06-08-2009 10:44 AM


All times are GMT +5.5. The time now is 11:03 AM.