Solved

java.util.Date how to convert to my format

Posted on 2002-05-30
5
212 Views
Last Modified: 2010-03-31
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
Comment
Question by:kcwang7
  • 2
  • 2
5 Comments
 

Expert Comment

by:omers
ID: 7044331
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
 
LVL 35

Expert Comment

by:girionis
ID: 7044338
 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
 

Accepted Solution

by:
omers earned 50 total points
ID: 7044340
Oops,

it should be

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

Hope this helps, Omer.
0
 
LVL 35

Expert Comment

by:girionis
ID: 7044352
 omers comment is a better solution and does not involve string manipulation. I would go with it.
0
 

Author Comment

by:kcwang7
ID: 7045765
girionis's solution is helpful too! how can I give some pts to girionis as well?
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
The viewer will learn how to implement Singleton Design Pattern in Java.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now