2

I have a String like "2015-07-16 17:07:21" . I want to convert it into a Date of same format. I tried something like this:

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:MM:ss");
Date date = sdf.parse("2015-07-16 17:07:21"); 

But output was different format like Thu Jul 16 17:00:21 IST 2015. How can I get it work as i need. can anyone help me. I know this might be a duplicate but i didn't find any luck.

Nathan Hughes
  • 94,330
  • 19
  • 181
  • 276
sparrow
  • 1,825
  • 6
  • 24
  • 35
  • 6
    a date does not have a format, it has a value representing a point in time. if you want to display it in some format you should use SimpleDateFormat – guleryuz Jul 16 '16 at 12:15
  • 1
    How can i do that. Can you please give me an example or a link to refer? – sparrow Jul 16 '16 at 12:17
  • When you want it to be formatted different, define another SimpleDateformat with the pattern you need and format the date with it. To be clear: You have one SimpleDateformat parsing the input and one for printing the output. – Henning Luther Jul 16 '16 at 13:00
  • 1
    At least two answers have sort of stated this, let me make it more explicit: A Date does not "have a format". A date is a (big) integer, the number of milliseconds since a time in the past. SimpleDateFormat is one (easy) way to either transform a String from a particular format into a date (with `parse()`), or transform a Date into a String (with `format()`). – arcy Jul 16 '16 at 13:15
  • I recommend you avoid the `SimpleDateFormat` class. It is not only long outdated, it is also notoriously troublesome. Today we have so much better in [`java.time`, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. May 23 '18 at 10:35

4 Answers4

11

From the Java API https://docs.oracle.com/javase/8/docs/api/java/util/Date.html

The class Date represents a specific instant in time, with millisecond precision.

Try to understand the difference/connection of Date and DateFormat.

public static void main(String [] args) throws ParseException{
    String dateString = "2015-07-16 17:07:21";
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    // use SimpleDateFormat to define how to PARSE the INPUT
    Date date = sdf.parse(dateString);

    // at this point you have a Date-Object with the value of
    // 1437059241000 milliseconds
    // It doesn't have a format in the way you think

    // use SimpleDateFormat to define how to FORMAT the OUTPUT
    System.out.println( sdf.format(date) );

    sdf = new SimpleDateFormat();
    System.out.println( sdf.format(date) );

  // ....
}

Output: (Please note that the Date stays the same, just its representation (format) changes)

2015-07-16 17:07:21
7/16/15 5:07 PM
sinclair
  • 2,812
  • 4
  • 24
  • 53
0

try format string like: yyyy-MM-dd HH:mm:ss

M : month, m: minute

public static void main(String [] args){
    String dateString = "2015-07-16 17:07:21";
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Date date;
    try {
        date = sdf.parse(dateString);
    } catch (ParseException e) {
        e.printStackTrace();
        return;
    } 
    System.out.println(sdf.format(date));
    System.out.println(dateString.equals(sdf.format(date)));
}

also, a date does not have a format, it has a value representing a point in time. if you want to display it in some format you should use SimpleDateFormat

guleryuz
  • 2,714
  • 1
  • 15
  • 19
0

Try to use this:

    public static void main(String[] args) {
    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    String dateInString = "2015-07-16 17:07:21";
    try {

        Date date = formatter.parse(dateInString);

        System.out.println(date);
        System.out.println(formatter.format(date));

    } catch (ParseException e) {
        e.printStackTrace();
    }
}

Output:

Thu Jul 16 17:07:21 IST 2015
2015-07-16 17:07:21

Note: For complete date and time patterns, please refer to this http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html

CK Wolfe
  • 51
  • 1
  • 3
-1

See this code:

public static void main(String [] args){
    String dateString = "2015-07-16 17:07:21";
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Date date;
    try {
        date = sdf.parse(dateString);
    } catch (ParseException e) {
        e.printStackTrace();
        return;
    } 
    System.out.println(date); // Displays date in it's default format Jul 16 17:00:21 IST 2015
    System.out.println(dateString.equals(date)); // Returns false since 'Jul 16 17:00:21 IST 2015' is not equal to dateString (2015-07-16 17:07:21)
}

Date class always stores date in its default format. The SimpleDateFormat helps to format date using its object. It doesn't make any changes to the date class object. Rather it converts the Date class to User defined format which doesn't have any effect on the Date class object. I guess it is clear.

CodeIt
  • 3,492
  • 3
  • 26
  • 37