Re: Help with Date and SimpleDateFormat

From: VisionSet (spam_at_ntlworld.com)
Date: 10/21/03


Date: Tue, 21 Oct 2003 12:14:52 +0100


"Kyote" <looking@you.com> wrote in message
news:ksn8pv44ct8m0gkt3tap778p61v3c9kqme@4ax.com...

> What I was trying to do was learn to understand Date. Now, instead of
> being passed a string and converting it into a date, I find that I am
> being passed a Calendar object. They changed things on me.
>
> So now I have some different problems. Here's what I have so far.
>
> public void setRequestedDate(Calendar param) {
> String c =
> param.get(Calendar.MONTH)
> + "/"
> + param.get(Calendar.DAY_OF_MONTH)
> + "/"
> + param.get(Calendar.YEAR)
> + " "
> + param.get(Calendar.HOUR_OF_DAY)
> + ":"
> + param.get(Calendar.MINUTE);
>
> }
>
> I have no clue if I'm doing this right. I'd really like to be able to
> create a main to test this out so I can continue to experiment, but,
> I'm at a loss as to how to do so. Please, any help would be highly
> appreciated!!
>
>

At one point you said this:

>Okay, what I'm trying to do is to send a string and convert it to a
>date and then format it. Apparently I don't understand the API at
>ALL.... :(

this is the only time you have really tried to say what you want to do. Is
this still what you want? Because if it is, it seems that you now have it.
If you have a Calendar object ie param, then you just get a Date object with
getTime(). If you want to format it then just do:

> >SimpleDateFormat sdf = new SimpleDateFormat("M/d/yyyy hh:mm");
> >String dd = sdf.format(param.getTime());
> >System.out.println(dd);

You can do all that manual concatenation you are doing, but the Format
classes make this unnecessary, hence the new pattern above: "M/d/yyyy hh:mm"

With the main you require to test just do:

public static void main(String[] args) {

    ThisClass a = new ThisClass();
    a.setRequestedDate(new GregorianCalendar());
}

but your setRequestedDate method is not setting anything!
Is this what you want to do? Set a Calendar object to a certain date?

Perhaps you could try to express clearly what you want to do!

-- 
Mike W


Relevant Pages

  • Re: blanking a java.util.Date in 1.5
    ... a Calendar with a Timezone to make all the fields to make a String. ... The fields are in the Calendar object. ... may I suggest that you please use your own SDF ... noon) is the right way to display AM/PM hours and not 00:00 for midnight. ...
    (comp.lang.java.programmer)
  • Re: Lost in "Date" maze
    ... >> retrieve this into a java.sql.Date object. ... >> display the month and year from it in a yyyymm format. ... >> how do I format the string that I need? ... Calendar object from it. ...
    (comp.lang.java.programmer)
  • Re: Fehler im Java melden?
    ... A date in a formated pattern will be convert in to a new date format. ... @param source_picture ... @return the date formated to target_picture */ public static String convertDate(final String date, final String sourcePicture, ...
    (de.comp.lang.java)
  • Problemas with function Format()
    ... The macro that I'm using receives the param "Dia" in DATE format but ... I'm not sure if this is the best option or if I should change to string ... How can I include my param "Dia" in the appropiate format???. ...
    (microsoft.public.excel.programming)
  • Re: Problems with .Refresh BackgroundQuery:=False
    ... The macro that I'm using receives the param "Dia" in DATE format but ... I'm not sure if this is the best option or if I should change to string ... How can I include my param "Dia" in the appropiate format???. ...
    (microsoft.public.excel.programming)