English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

import java.text.*;
import java.util.*;

public class TIME {
public static void main(String[] args) throws ParseException {

String s = "2003-09-30 12:30:00";
Date date = new Date();
date = new SimpleDateFormat
("yyyy-MM-dd HH:mm:ss").parse (s);

SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");

System.out.println(date);
}
}

-
Output
-
Tue Sep 30 12:30:00 SGT 2003

Why the above code have output show as above one? Is it should have output that show as below one?

-
Output I want
-
2003-09-30 12:30:00

How i make it to have Output I want?

2007-08-20 16:19:18 · 2 answers · asked by scottcky1985 3 in Computers & Internet Programming & Design

2 answers

A DateFormat (including SimpleDateFormat) can be used both to parse *into* a Date, and to format *outfrom* a Date. Different formats can be used for each.

You used one SimpleDateFormat to parse the string into date.
The other SimpleDateFormat named 'format' was never used.
System.out.println(date);
used the .toString() method of Date, which yields the default output you saw.

Try:
System.out.println( format.format(date) );

Hope I made this easy to understand.

p.s.
For creating dates you *might* find it easier to use
GregorianCalendar(int year, int monthOffset, int day) like:
Date date = new GregorianCalendar( 2003, 08, 30 ).getTime();
Note that the month is zero offset: 08 is September

Edit: Just corrected the ". . . GregorianCalendar . . ." line.

2007-08-21 03:30:37 · answer #1 · answered by oldguy 4 · 3 1

You created a SimpleDateFormat, but you are not using it to format the date.

If you do "System.out.println( date)" you just get the default toString() method on the date -- it has no way to "know" that you created an instance of SimpleDateFormat.

You need to use SimpleDateFormat.format() to convert your date to a String in the specified format, then call System.out.println() with that String.

Try this:
======================
// Setup
String s = "2003-09-30 12:30:00";
Date date = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" ).parse(s);
// Create the output format
SimpleDateFormat outFmt = new SimpleDateFormat( "yyyy-MM-dd HH:mm");
// Print the formatted date
System.out.println( outFmt .format( date));
======================

When I execute the program above, the output is:

2003-09-30 12:30

======================
Other notes:

You don't need to create a new Date().
SimpleDateFormat. parse() returns a new instance of Date anyway.

So instead of:

Date date = new Date();
date = new SimpleDateFormat
("yyyy-MM-dd HH:mm:ss").parse (s);

You can just write:

Date date = new SimpleDateFormat
("yyyy-MM-dd HH:mm:ss").parse (s);

Also, good job on at least trying to work out the program on your own before asking the question. It's refreshing to see someone who's not asking for their entire program to be written for them.

2007-08-20 16:27:19 · answer #2 · answered by McFate 7 · 0 0

fedest.com, questions and answers