import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class ConvertDatesBetweenFormatsWithSimpleDateFormat {
public static void main(String[] args) {
try {
String dateStr = "12:30";
DateFormat srcDf = new SimpleDateFormat("hh:mm");
Date date = srcDf.parse(dateStr);
DateFormat destDf = new SimpleDateFormat("HH:mm:ss");
dateStr = destDf.format(date);
System.out.println("Converted date is : " + dateStr);
}
catch (ParseException e) {
e.printStackTrace();
}
}
}
I am trying to convert date format from hh:mm to HH:mm:ss. While this code is working fine for all the time, its not givig the required result for time between 12-1. Eg: for 12:30 its giving 00:30(it should be 12:30 instead), while for 1:30 it is giving as 13:30. What changes should be done in this case?