• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 229
  • Last Modified:

java.util.Date how to convert to my format

Dear All,

I have a java.util.Date object, which I print it out
the default is (Thu May 30 20:18:46 HKT 2002). e.g.
Date d = new Date();
System.out.println(d)

how should I write the code, so that I can print
out (May 30)? i.e. the Month and the date only.

Very Urgent!

Thanks!
0
kcwang7
Asked:
kcwang7
  • 2
  • 2
1 Solution
 
omersCommented:
Hi,

Very easy. You need to create a class formatter = new java.text.SimpleDateFormat("MM dd");
and then String myString = formatter.format(date);

where date is your Date object.

Omer.
0
 
girionisCommented:
 A quick and "dirty" solution:

            java.util.Date date = new java.util.Date();
          DateFormat df = DateFormat.getDateTimeInstance(DateFormat.DEFAULT, DateFormat.DEFAULT, Locale.US);
          System.out.println(df.format(date).substring(0, df.format(date).indexOf(',')));

  Not the best though. Other experts might have a better recommendation.

  Hope it helps.
0
 
omersCommented:
Oops,

it should be

SimpledDateFormat format = new SimpleDateFormat("MMMM dd");

Hope this helps, Omer.
0
 
girionisCommented:
 omers comment is a better solution and does not involve string manipulation. I would go with it.
0
 
kcwang7Author Commented:
girionis's solution is helpful too! how can I give some pts to girionis as well?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now