1

I am having TimeStamp as a String input. I'll have to convert it into a String of another format.

For example:

input string : 2013-12-23 20:59:15.0

output string: 2013/12/23

So I wrote a small Java program to do that :

public class test {
    public static void main(String[] args) throws ParseException
    {
        DateFormat dateFormat = new SimpleDateFormat("yyyy-mm-dd hh:mm:ss.SS");
        String x ="2013-12-23 20:59:15.0";

        Date parsedDate = dateFormat.parse(x);

        Format formatter = new SimpleDateFormat("yyyy/MM/dd");

        String y = formatter.format(parsedDate);

        System.out.println(y);
    }
}

Now no matter what the input is, the month value always comes back to be 1.

For example :

input string : 2013-12-23 20:59:15.0

output string: 2013/01/23

Another example :

input string : 2012-05-23 20:59:15.0

output string: 2012/01/23

Am I missing something here ?

Michael Yaworski
  • 13,410
  • 19
  • 69
  • 97
user2434
  • 6,339
  • 18
  • 63
  • 87

3 Answers3

1

yyyy-mm-dd should be yyyy-MM-dd

Basically, you converting the month element to minutes.

m Minute in hour Number 30
M Month in year Month July; Jul; 07

Take a look at SimpleDateFormat for more details

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
1
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.SS");
Paul Draper
  • 78,542
  • 46
  • 206
  • 285
0
import java.sql.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;

public class Dateformat

{
    public static void main(String [] args) throws ParseException
    {
        String oldstring = "2013-11-15 00:00:00.0";

        java.util.Date date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S").parse(oldstring);

        String newstring = new SimpleDateFormat("yyyy-MM-dd").format(date);

        System.out.println(newstring);

    }

}

output

2013-11-15

SUBZ
  • 139
  • 1
  • 7