Subtracting two SimpleDateFormat to get the difference in days - java

I'm making a simple app where i get I get time is as a long and the current time from calendar.getInstance() (the current time).
I would like to check the difference in days between the 2 dates.
Long date1 = 1499175346756l;
SimpleDateFormat dateFormat = new SimpleDateFormat("dd.MM.yy.HH.mm.ss");
Calendar c = Calendar.getInstance();
String formattedDate = dateFormat.format(c.getTime());

If i am not wrong, you have both the dates in "Long type", than you can try the following code
Calendar firstDate = Calendar.getInstance();
Calendar lastDate = Calendar.getInstance();
Date startDate = firstDate.getTime();
Date endDate = lastDate.getTime();
long startTime = startDate.getTime();
long endTime = endDate.getTime();
long diffTime = endTime - startTime;
long diffDays = diffTime / (1000 * 60 * 60 * 24);

Related

Converting difference between 2 dates to days in java 7 [duplicate]

This question already has answers here:
Calculating days between two dates with Java
(16 answers)
Closed 3 years ago.
I want to compare 2 dates in java and need to convert the difference between the 2 dates to days
//Setting up date
Calendar cal = Calendar.getInstance();
cal.set(2019, 5, 16);
Date d = cal.getTime();
Output would be something like this : Sun Jun 16 11:04:57 UTC 2019
//Getting the current date instance
Calendar cal1 = Calendar.getInstance();
Date d1 = cal1.getTime();
Output would be something like this : Mon Jul 08 11:04:57 UTC 2019
Need to get the difference between d & d1 in days.
Thanks in advance for taking your time to provide solution
Here, you just have to do simple math.
Calendar start = Calendar.getInstance();
Calendar end = Calendar.getInstance();
start.set(2010, 7, 23);
end.set(2010, 8, 26);
Date startDate = start.getTime();
Date endDate = end.getTime();
long startTime = startDate.getTime();
long endTime = endDate.getTime();
long diffTime = endTime - startTime;
long diffDays = diffTime / (1000 * 60 * 60 * 24);
DateFormat dateFormat = DateFormat.getDateInstance();
System.out.println("The difference between "+
dateFormat.format(startDate)+" and "+
dateFormat.format(endDate)+" is "+
diffDays+" days.");
This will not work when crossing daylight savings time (or leap seconds) and might as well not give the expected results when using different times of day. You can modify it like this:
start.add(Calendar.DAY_OF_MONTH, (int)diffDays);
while (start.before(end)) {
start.add(Calendar.DAY_OF_MONTH, 1);
diffDays++;
}
while (start.after(end)) {
start.add(Calendar.DAY_OF_MONTH, -1);
diffDays--;
}
Hope this helps. Good luck.
Simplest way:
public static long getDifferenceDays(Date d, Date d1) {
long diff = d1.getTime() - d.getTime();
return TimeUnit.DAYS.convert(diff, TimeUnit.MILLISECONDS);
}

How do i get Milliseconds in GMT?

YhTimestamp timestamp = new YhTimestamp();
System.out.println(timestamp.getTime());
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
long time = cal.getTimeInMillis();
System.out.println(cal.getTime());
System.out.println(time);
You will get always timestamp in GMT either you use Calendar or Date or System.getCurrenttimemills()

how to convert timestamp into hoursminandsec in java

I have a unix timestamp. I wanted to convert into hours,min and seconds.I wanted to acheive it in java.I tried this .But I am not sure how do i have to concatenate it to hours+min+sec
int day = (int)TimeUnit.SECONDS.toDays(timeStamp);
long hours = TimeUnit.SECONDS.toHours(timeStamp) - (day *24);
long minute = TimeUnit.SECONDS.toMinutes(timeStamp) - (TimeUnit.SECONDS.toHours(timeStamp)* 60);
long second = TimeUnit.SECONDS.toSeconds(timeStamp) - (TimeUnit.SECONDS.toMinutes(timeStamp) *60);
thanks,
Ramya.
You can use the Calendar class for this. You can format the time using SimpleDateFormat
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(timeStamp);
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
String dateString = sdf.format(calendar.getTime());
Date date = new Date ();
date.setTime((long)unix_time*1000);
SimpleDateFormat df = new SimpleDateFormat("hh:mm:ss a",Locale.ENGLISH);
System.out.println(df.format(date));

mean time of two string time in java

I have two times variable as string
want to find the mean time. please help me
inTime = shift.getInTime()+":00";
outTime = shift.getOutTime()+":00";
SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss");
Date d1=df.parse(inTime);
Date d2 = df.parse(outTime);
long date1InMilSec=d1.getTime();
long date2InMilSec=d2.getTime();
long half =date1InMilSec + ((date2InMilSec - date1InMilSec) / 2);
long minute = (half / (1000 * 60)) % 60;
long hour = (half / (1000 * 60 * 60)) % 24;
String time = String.format("%02d:%02d", hour, minute);
First of all, if I use your code and set fixed time values, then I get 11:30:00 instead of your 07:00:00, so there is maybe something else wrong with inTime and outTime.
Since I get 11:30:00 there is maybe something wrong with your calculation of hour and minute, but I won't bother that. Let's use a new Date instance to do the conversion:
SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss");
Date d1 = df.parse("09:30:00");
Date d2 = df.parse("15:30:00");
long date1InMilSec = d1.getTime();
long date2InMilSec = d2.getTime();
long half = date1InMilSec + ((date2InMilSec - date1InMilSec) / 2);
Date meanTime = new Date(half); // new Date instance, instead of own calculation
String time = df.format(meanTime);
System.out.println(time);
This code prints:
12:30:00
String inTime = "09:30:00";
String outTime = "15:30:00";
SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss");
Date dateIn = df.parse(inTime);
Date dateOut = df.parse(outTime);
long dateInMill = dateIn.getTime();
long dateOutMill = dateOut.getTime();
long dateMiddleMill = dateInMill + ((dateOutMill - dateInMill) / 2);
Date dateMiddle = new Date(dateMiddleMill);
System.out.println(df.format(dateMiddle));

Add hours and Minutes to a certain time java

I have String initialTime= "10:30:00"
I am converting it into time like so:-
DateFormat sdf = new SimpleDateFormat("hh:mm:ss");
Date date = sdf.parse(initialTime);
Time time = new Time(date.getTime());
int initHour = time.getHours()
int initMind = time.getMinutes();
Further I have two values;
int hour, mins;
The hour value can be anything from 0-10;
mins value can be 0,15,30,45.
The user selects a time by selecting hour and mins. As soon as the user selects the values they should get added to the initialTimeand shown in finalTIme
So if:-
hour=0 and mins=15 then finalTIme=10:45:00
hour=0 and mins=35 then finalTIme=11:00:00
hour=0 and mins=45 then finalTIme=11:15:00
I tried doing something like:-
if(hour==0 && mins==0)
{
finalTime = initialTime;
}
else if(hour==0 && mins>0 && mins <30)
{
mins = initMins + mins
}
else if(hour==0 && mins>0 && mins >=30)
{
hours = hours+1;
mins = mins-60;
}
But I did not get the required output. HOw can I do it in a less complicated manner?
You can use java.util.Calendar class:
DateFormat sdf = new SimpleDateFormat("hh:mm:ss");
Date date = sdf.parse(initialTime);
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.HOUR_OF_DAY, hours);
cal.add(Calendar.MINUTE, minutes);
date = cal.getTime();
You might also consider the new Date API from JDK 8.

Categories