[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 224
  • 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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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