Skip to content

How to properly format the date?

The function shown below returns the date, e.g. “Sat Sep 8 00:00 PDT 2010”. But I expected to get the date in the following format “yyyy-MM-dd HH:mm”. What’s wrong in this code?

String date = "2010-08-25";
String time = "00:00";

Also in one laptop the output for,e.g. 23:45 is 11:45. How can I define exactly the 24 format?

private static Date date(final String date,final String time) {
       final Calendar calendar = Calendar.getInstance();
       String[] ymd = date.split("-");
       int year = Integer.parseInt(ymd[0]);
       int month = Integer.parseInt(ymd[1]);
       int day = Integer.parseInt(ymd[2]);
       String[] hm = time.split(":");
       int hour = Integer.parseInt(hm[0]);
       int minute = Integer.parseInt(hm[1]);
       SimpleDateFormat dateFormat =  new SimpleDateFormat("yyyy-MM-dd HH:mm");
       Date d = calendar.getTime();
       String dateString= dateFormat.format(d);
       Date result = null;
       try {
            result = (Date)dateFormat.parse(dateString);
       } catch (ParseException e) {
       return result;


How did you print out the return result? If you simply use System.out.println(date("2010-08-25", "00:00") then you might get Sat Sep 8 00:00 PDT 2010 depending on your current date time format setting in your running machine. But well what you can do is:

Date d = date("2010-08-25", "00:00");
System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm").format(d));

Just curious why do you bother with this whole process as you can simple get the result by concatenate your initial date and time string.