-2

I am facing the following problem, I have a date dd-mm-yyyy and I need to convert it to yyyy-MM-dd HH:mm (i.e. with the given date and current hours and minutes).

Example : Given date : 11-06-2014 , I should get 2014-06-11 09:30

 public String previousDateString(String dateString)
        throws ParseException {
    // Create a date formatter using your format string
    DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");

    // Parse the given date string into a Date object.
    // Note: This can throw a ParseException.
    Date myDate = dateFormat.parse(dateString);

    // Use the Calendar class to subtract one day
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(myDate);
    calendar.setTime(myDate);
    calendar.add(Calendar.DAY_OF_YEAR, -1);

    // Use the date formatter to produce a formatted date string
    Date previousDate = calendar.getTime();
    String result = dateFormat.format(previousDate);

    return result;
}   

I have tried this code but I am getting 2014-06-10 00:00

Jim
  • 3,254
  • 1
  • 19
  • 26
LMK
  • 2,882
  • 5
  • 28
  • 52
  • You get 00:00 because you're not setting current time anywhere in your code. Also I don't know why you're substracting one day ¿? – m0skit0 Jun 12 '14 at 16:01

3 Answers3

0

You get 00:00 because the information you pass in 11-06-2014 does not contain hours or minutes.

If you want the hours:minutes in your converted time, then you need to either add more information to your input or get the current hour/minute.

Kevin L
  • 1,066
  • 3
  • 12
  • 21
0

You have to use two Calendar instances, because the Calendar setTime method sets the hours and minutes to zero.

package com.ggl.testing;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class FormatDate {

    private static final DateFormat inputDateFormat =
            new SimpleDateFormat("dd-MM-yyyy");
    private static final DateFormat outputDateFormat = 
            new SimpleDateFormat("yyyy-MM-dd HH:mm");

    private Calendar calendar;

    public String previousDateString(String dateString) 
            throws ParseException {
        Calendar now = Calendar.getInstance();

        calendar = Calendar.getInstance();
        Date myDate = inputDateFormat.parse(dateString);
        calendar.setTime(myDate);
        calendar.set(Calendar.HOUR_OF_DAY, 
                now.get(Calendar.HOUR_OF_DAY));
        calendar.set(Calendar.MINUTE, 
                now.get(Calendar.MINUTE));

        return outputDateFormat.format(calendar.getTime());
    }

    public static void main(String[] args) {
        String dateString = "10-06-2014";
        try {
            String output = 
                    new FormatDate().previousDateString(dateString);
            System.out.println(output);
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }

}

Output - 2014-06-10 10:47

Gilbert Le Blanc
  • 50,182
  • 6
  • 67
  • 111
0

If you have the option of using Java 8 you can write the solution like this:

private LocalTime startOfDay = LocalTime.of(9, 30);

public String previousDateString(String dateString) {
    LocalDate yesterday = LocalDate.parse(dateString, DateTimeFormatter.ofPattern("dd-MM-yyyy"));
    LocalDateTime yesterdayTime = LocalDateTime.of(yesterday, startOfDay);
    return yesterdayTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm"));
}
Jim
  • 3,254
  • 1
  • 19
  • 26