Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add current time to a previous date in java?

Tags:

java

date

I was trying to add current time into previous date. But it was adding in current date with time not with previous date.

see my bellow code:

Date startUserDate = ;//this is my previous date object;
startUserDate.setTime(new Date().getTime());// here i'm trying to add current time in previous date.
System.out.println("current time with previous Date :"+startUserDate);

In previous date there is no time and i want to add current time in previous date.I can do this, please help me out.

like image 237
Aadi Avatar asked Jul 01 '15 07:07

Aadi


People also ask

How can I add previous date in Java?

Use LocalDate 's plusDays() and minusDays() method to get the next day and previous day, by adding and subtracting 1 from today.

How do you set a specific time in Java?

The setTime() method of Java Date class sets a date object. It sets date object to represent time milliseconds after January 1, 1970 00:00:00 GMT. Parameters: The function accepts a single parameter time which specifies the number of milliseconds. Return Value: It method has no return value.

How do I add 7 days to Java Util date?

Adding Days to the given Date using Calendar class Add the given date to the calendar by using setTime() method of calendar class. Use the add() method of the calendar class to add days to the date. The add method() takes two parameter, i.e., calendar field and amount of time that needs to be added.


3 Answers

Use calendar object

Get instance of calendar object and set your past time to it Date startUserDate = ;

    Calendar calendar = Calendar.getInstance();
    calendar.settime(startUserDate);

Create new calendar instance

     Calendar cal = Calendar.getInstance();
     cal.settime(new Date());

format the date to get string representation of time of current date

        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
        String currentdate =  sdf.format(cal.getTime());

split that string to get hour minute and second object

                String hh = expiry.split(":")[0];
                String mm = expiry.split(":")[1];
                String ss = expiry.split(":")[2];

add it to the previous calendar object

    calendar .add(Calendar.HOUR_OF_DAY, hh);
    calendar .add(Calendar.MINUTE, mm);
    calendar .add(Calendar.SECOND, ss);

this date will have current time added to your date

   Date newDate = calendar.getTime;
like image 130
kirti Avatar answered Oct 19 '22 05:10

kirti


Use Calendar:

  • first set the date/time of the first calendar object to the old date

  • object use as second Calendar object to set the current time on the

  • first calendar object then convert it back to date

as follow:

//E.g. for startUserDate 
Date startUserDate = new Date(System.currentTimeMillis() - (24L * 60L * 60L * 1000L) - (60L * 60L * 1000L));//minus 1 day and 1 hour
Calendar calDateThen = Calendar.getInstance();
Calendar calTimeNow = Calendar.getInstance();
calDateThen.setTime(startUserDate);
calDateThen.set(Calendar.HOUR_OF_DAY, calTimeNow.get(Calendar.HOUR_OF_DAY));
calDateThen.set(Calendar.MINUTE, calTimeNow.get(Calendar.MINUTE));
calDateThen.set(Calendar.SECOND, calTimeNow.get(Calendar.SECOND));
startUserDate = calDateThen.getTime();
System.out.println(startUserDate);

The second Calendar object calTimeNow can be replaced with Calendar.getInstance() where it is used.

like image 28
TungstenX Avatar answered Oct 19 '22 04:10

TungstenX


You can do it using DateFormat and String, here's the solution that you need:

Code:

DateFormat df = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
String timeString = df.format(new Date()).substring(10); // 10 is the beginIndex of time here

DateFormat df2 = new SimpleDateFormat("MM/dd/yyyy");
String startUserDateString = df2.format(startUserDate);

startUserDateString = startUserDateString+" "+timeString;
// you will get this format "MM/dd/yyyy HH:mm:ss" 

//then parse the new date here
startUserDate = df.parse(startUserDateString);

Explanation:

Just convert the current date to a string and then extract the time from it using .substring() method, then convert your userDate to a string concatenate the taken time String to it and finally parse this date to get what you need.

Example:

You can see it working in this ideone DEMO.

Which takes 02/20/2002 in input and returns 02/20/2002 04:36:14 as result.

like image 25
cнŝdk Avatar answered Oct 19 '22 06:10

cнŝdk