Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Date and Time convert

Tags:

java

date

time

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...

like image 535
user3736748 Avatar asked Apr 19 '26 04:04

user3736748


1 Answers

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";
like image 166
Robby Cornelissen Avatar answered Apr 21 '26 00:04

Robby Cornelissen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!