Solved

java.util.Date how to convert to my format

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
rhino JavaScript import, load 25 84
Should localization be done inside spring controller 5 26
servlet example issue 6 40
iterator/ListIterator approach 17 27
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

810 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