Solved

java.util.Date how to convert to my format

Posted on 2002-05-30
5
216 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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Java 8 to Java 6 8 32
import as existing maven project 3 34
What browser will run Java? 7 122
American Express @Work site and Java 4 63
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This video teaches viewers about errors in exception handling.

713 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