user3736748
user3736748

Reputation: 205

Java Date and Time convert

I have this piece of code below:

private final static String DATE_TIME_FORMAT_FILE = "dd_MM_YYYY_HH_mm";
private final static String DATE_TIME_FORMAT_DATA = "dd/MM/YYYY HH:mm";

String sdfDateTimeData = null;
    Calendar calData = Calendar.getInstance();
    SimpleDateFormat sdfFile = new SimpleDateFormat(DATE_TIME_FORMAT_FILE);
    SimpleDateFormat sdfData = new SimpleDateFormat(DATE_TIME_FORMAT_DATA);
    String sdfDateTimeFile = sdfFile.format(calData.getTime());

    try {
        Date dt = sdfFile.parse(sdfDateTimeFile);
        sdfDateTimeData = sdfData.format(dt);
    } catch (ParseException e1) {
        e1.printStackTrace();
    }

I want to convert same date and time into 2 diferent formats. It runs properly but he 2nd format changes the date:

OUTPUT:

16_06_2014_23_11

29/12/2014 23:11

As you can see the date changes from 16_06_2014 to 29/12/2014. Anyone know why???

Thank you...

Upvotes: 3

Views: 101

Answers (2)

Robby Cornelissen
Robby Cornelissen

Reputation: 97120

The uppercase Y stands for "week year". You need the lowercase y in your format.

Here's the javadoc for SimpleDateFormat. It shows you how to create your patterns and this is what your patterns should look like:

private final static String DATE_TIME_FORMAT_FILE = "dd_MM_yyyy_HH_mm";
private final static String DATE_TIME_FORMAT_DATA = "dd/MM/yyyy HH:mm";

Upvotes: 5

ngrashia
ngrashia

Reputation: 9894

Try using yyyy as:

private final static String DATE_TIME_FORMAT_FILE = "dd_MM_yyyy_HH_mm";
private final static String DATE_TIME_FORMAT_DATA = "dd/MM/yyyy HH:mm";

Instead of dd/MM/YYYY

Upvotes: 0

Related Questions