Getting pure time element from long timestamp - java

In my java (actually, Android, but it's irrelevant here) project, the user can enter start and end time for some activity and the system automatically computes the duration. On button press, I use System.currentTimeMillis() to get the current timestamp.
For reasons outside the scope of this question, I need to discard the date part of the timestamp and only store the time one. Easy, I thought, just divide by the number of milliseconds in a day and take the remainder:
long currentTimestamp = System.currentTimeMillis();
long timeOnly = currentTimeStamp % (1000 * 60 * 60 * 24);
This almost works - except it produces timestamp one hour off. Here's an example:
DateFormat timeFormat = DateFormat.getTimeInstance(DateFormat.SHORT, Locale.US);
long currentTimestamp = System.currentTimeMillis();
long timeOnly = currentTimestamp % (1000 * 60 * 60 * 24);
System.out.println("Full value: " + timeFormat.format(currentTimestamp));
System.out.println("Time only: " + timeFormat.format(timeOnly));
This code prints:
Full value: 10:53 PM
Time only: 11:53 PM
Full value: 11:19 PM
Time only: 12:19 AM
While I can just subtract one hour from the timestamp, I want to actually understand the reason why this is happening.

Try:
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date(System.currentTimeMillis()));
calendar.set(Calendar.MONTH, 0);
calendar.set(Calendar.YEAR, 0);
calendar.set(Calendar.DAY_OF_MONTH, 0);
System.out.println("Full value: " + timeFormat.format(currentTimestamp));
System.out.println("Time only: " + timeFormat.format(calendar.getTimeInMillis()));
The Calendar instance will use your current Timezone and all setters will take the change the timezone into consideration. Thus, it should work.
Note:
The question remains, why you want to do that. Do you just want to store time information in a database, instead of having the 'full date' stored? If you want to do that, than you could map your current locale time to a UTC time (instead of using currentTimeMillis()).

Related

get days starting from epoch

I want count of days starting from epoch(1970-01-01). I tried with joda-time
try {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date1 = sdf.parse("2013-05-03 07:00:00");
Date date2 = sdf.parse("2013-05-03 23:30:00");
MutableDateTime epoch = new MutableDateTime();
epoch.setDate(0); //Set to Epoch time
System.out.println("Epoch: " + epoch);
Days days1 = Days.daysBetween(epoch, new MutableDateTime(date1.getTime()));
Days days2 = Days.daysBetween(epoch, new MutableDateTime(date2.getTime()));
System.out.println("1) Days Since Epoch: " + days1.getDays());
System.out.println("2) Days Since Epoch: " + days2.getDays());
} catch (ParseException e) {
e.printStackTrace();
}
and using logic:
// Create Calendar instance
Calendar calendar1 = Calendar.getInstance();
Calendar calendar2 = Calendar.getInstance();
calendar2.setTime(date);
// Set the values for the calendar fields YEAR, MONTH, and DAY_OF_MONTH.
// calendar1.set(calendar1.YEAR, calendar1.MONTH, Calendar.DAY_OF_MONTH);
calendar1.set(1970, 1, 1);
/*
* Use getTimeInMillis() method to get the Calendar's time value in
* milliseconds. This method returns the current time as UTC
* milliseconds from the epoch
*/
long miliSecondForDate1 = calendar1.getTimeInMillis();
long miliSecondForDate2 = calendar2.getTimeInMillis();
// Calculate the difference in millisecond between two dates
long diffInMilis = miliSecondForDate2 - miliSecondForDate1;
/*
* Now we have difference between two date in form of millsecond we can
* easily convert it Minute / Hour / Days by dividing the difference
* with appropriate value. 1 Second : 1000 milisecond 1 Hour : 60 * 1000
* millisecond 1 Day : 24 * 60 * 1000 milisecond
*/
long diffInSecond = diffInMilis / 1000;
long diffInMinute = diffInMilis / (60 * 1000);
long diffInHour = diffInMilis / (60 * 60 * 1000);
long diffInDays = diffInMilis / (24 * 60 * 60 * 1000);
if(logger.isInfoEnabled()) {
logger.info("Difference in Seconds : " + diffInSecond);
logger.info("Difference in Minute : " + diffInMinute);
logger.info("Difference in Hours : " + diffInHour);
logger.info("Difference in Days : " + diffInDays);
}
I am getting diff result for both of this. can somebody help where i am wrong.
thanks.
The difference between 17006 and 17007 is one day. This difference very likely comes from 7:00 and 23:30 in your time zone are on different days in some other time zone, say, UTC. Or the other way around, those times in UTC happen to be on different days in your time zone. Therefore the count is off by one. I don’t know JodaTime, so I cannot give you the precise details.
The difference between 16975 and 17006 is 31 days or a full month. I can tell you exactly where this comes from. Calendar months are 0-based: January is month 0, February is 1, etc. So calendar1.set(1970, 1, 1) sets your Calendar to February 1, 1970, 31 days after the epoch. Use calendar1.set(1970, Calendar.JANUARY, 1) instead. You will also want to control the hours, minutes and seconds of the Calendar. You may call clear() before set() to make sure the time is at midnight.
If you can use Java 8, you may do:
DateTimeFormatter format = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss");
OffsetDateTime t1 = LocalDateTime.parse("2013-05-03 07:00:00", format)
.atOffset(ZoneOffset.UTC);
OffsetDateTime t2 = LocalDateTime.parse("2013-05-03 23:30:00", format)
.atOffset(ZoneOffset.UTC);
System.out.println("1) Days Since Epoch: " + ChronoUnit.DAYS.between(Instant.EPOCH, t1));
System.out.println("2) Days Since Epoch: " + ChronoUnit.DAYS.between(Instant.EPOCH, t2));
This prints:
1) Days Since Epoch: 15828
2) Days Since Epoch: 15828
If you want to use a different time zone:
DateTimeFormatter format = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss");
ZonedDateTime t1 = LocalDateTime.parse("2013-05-03 07:00:00", format)
.atZone(ZoneId.systemDefault());
ZonedDateTime t2 = LocalDateTime.parse("2013-05-03 23:30:00", format)
.atZone(ZoneId.systemDefault());
System.out.println("1) Days Since Epoch: " + ChronoUnit.DAYS.between(Instant.EPOCH, t1));
System.out.println("2) Days Since Epoch: " + ChronoUnit.DAYS.between(Instant.EPOCH, t2));
You can fill in the desired time zone instead of ZoneId.systemDefault().

Java: get the number of seconds of a Calendar variable

I have a java.util.Calendar variable (like 2016-05-24 00:01:05.780) and I want to get the total of seconds of the time. In this example, I want to get 65 as result.
I tried to use myCalendarVariable.getTimeInMillis()/1000 but I got -2208977011 as result.
How can I do that?
You can try
variable.get(Calendar.HOUR_OF_DAY) * 60 * 60 + variable.get(Calendar.MINUTE) * 60 + variable.get(Calendar.SECOND)
Note that Calendar.HOUR_OF_DAY is used when you're using a 24-hour clock, whereas Calendar.HOUR should be used for 12-hour clocks. In this situation you want to use the former.
Try to use get function:
myCalendarVariable.get(HOUR_OF_DAY)* 3600 + myCalendarVariable.get(MINUTE) * 60 + myCalendarVariable.get(SECOND)
Not sure which scope of seconds you were looking for, so try one of these:
Calendar myCalendarVariable = Calendar.getInstance();
int secondsInHour = myCalendarVariable.get((Calendar.MINUTE) * 60) + myCalendarVariable.get(Calendar.SECOND);
int secondsOfDay = (myCalendarVariable.get(Calendar.HOUR_OF_DAY) * 60 * 60) + secondsInHour;
You can try this:
Calendar calendar = Calendar.getInstance();
calendar.setTime(yourdate);
int minutes = calendar.get(Calendar.MINUTE);
int seconds = minutes*60 + calendar.get(Calendar.SECOND);;
If you can convert your date into a LocalDateTime you can the seconds of day directly.
The convertion from Calendar to LocalDateTime has been omitted, as you might have the data in another format. This should be easy to amend to your needs.
LocalDateTime of = LocalDateTime.of(2016, 5, 24, 0, 1, 5, 780000000);
System.out.println("seconds of day: " + of.getLong(ChronoField.SECOND_OF_DAY));
output
seconds of day: 65

Can't get the number of days between 2 months - specific case

I have few dates in this format: 31/08/13 and I'm getting tehm from an xls file
What I need to do is to get the previous month and to calculate the days betwen these dates. I really don't know what to do.
Here is what I have tried:
code edited*
Calendar start = Calendar.getInstance();
Calendar end = Calendar.getInstance();
end.setTime(row.getCell(0).getDateCellValue());
start.add(start.MONTH,-1);
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);
if(diffDays < 0){
System.out.println(Math.abs(diffDays));
}
DateFormat dateFormatw = DateFormat.getDateInstance();
System.out.println("The difference between "+
dateFormatw.format(startDate)+" and "+
dateFormatw.format(endDate)+" is "+
diffDays+" days.");
But it seems wrong. I really can't get my mind on it as I'm so tired. I lost too many hours on this without luck.
I'm trying to get the same date but one month ago and to calculate the days, between these 2 dates endTime and startTime
Please help me!
I don't know what a ProdCalendar is, it's presumably something internal to your project. But the simple way to get the numbers of days between 2 dates, which is (I think) what you want, is this:
//First date
Date d1 = dateFormat.parse("31-10-13");
//Second date
Date d2 = dateFormat.parse("31-08-13");
//Interval:
long intervalMs = d1.getTime() - d2.getTime();
long intervalDays = intervalMs/(1000*60*60*24);
In your code, date22 is initialised just with new date() so it will be the current system time. You are comparing that (in a depracated way) to a date retrieved from the spreadsheet. I also notice that Calendar cal in this line near the start:
Calendar cal = Calendar.getInstance();
cal.setTime(row.getCell(0).getDateCellValue());
Is not used again, so appears to be redundant.
Your code is more complicated than it needs to be, so you need to step back and think about what you want to achieve. Simple code is good code.

Confused about date and time in Excel

I have Java time (ms since 1/1/1970 UTC) and would like to write that time to a csv file, so that Excel can correctly interpret and format it. I understand, that excel uses "serial date time" as a format - that is a floating point number, where the integer part gives the number of days since 1/1/1900 and the decimal part gives fractions of a day.
I fail to understand timezone and daylight saving time handling in this.
This page says that the excel epoch (1/1/1900) is based on the local (=computer creating the Excel file?) timezone. This means that a serial date does not indicate a unique instant in time without the info which computer timezone created it. Not what I would have chosen, but OK.
Now accepting this, I believed I could convert Java time to Excel serial date by the following Java code (nb: I'm in Zurich, CET timezone):
private static final long ONE_HOUR= 60L * 60 * 1000;
private static final long ONE_DAY = 24 * ONE_HOUR;
private static final long excelEpoch;
static{
Calendar cal;
cal = Calendar.getInstance(TimeZone.getTimeZone("Europe/Zurich"));
cal.set(Calendar.YEAR, 1900);
cal.set(Calendar.DAY_OF_YEAR, 1);
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
excelEpoch = cal.getTimeInMillis();
}
private static String formatForExcel(long time){
return ""+(time-excelEpoch)/(double)ONE_DAY;
}
Using this I can print out a few times:
public static void main(String[] args) {
String sep = "\t"; // csv field separator
SimpleDateFormat fmt = new SimpleDateFormat("HH:mm:ss d/M/yyyy");
fmt.setTimeZone(TimeZone.getTimeZone("Europe/Zurich"));
System.out.println("Time in ms since 1/1/1970 UTC"+ sep + "Time as string" + sep + "Excel serial" + sep + "Excel serial formatted by excel");
long startTime = 1332630000000L; // 25/3/2012 00:00 CET , shortly before change from winter time to DST
for (long t = startTime; t < startTime + 4*ONE_HOUR; t+=ONE_HOUR) {
System.out.println(t + sep + fmt.format(new Date(t)) + sep + formatForExcel(t) + sep + formatForExcel(t));
}
}
Which returns
Time in ms since 1/1/1970 UTC Time as string Excel serial Excel serial formatted by excel
1332630000000 00:00:00 25/3/2012 40991.0 40991.0
1332633600000 01:00:00 25/3/2012 40991.041666666664 40991.041666666664
1332637200000 03:00:00 25/3/2012 40991.083333333336 40991.083333333336
1332640800000 04:00:00 25/3/2012 40991.125 40991.125
Note that the change from winter time to DST happens in those hours (check second column, hour 2 is missing).
Now comes the confusion. If I paste this in excel, and for the last column choose "Format cells..." and then "Time" (any of the formats), it prints:
Time in ms since 1/1/1970 UTC Time as string Excel serial Excel serial formatted by excel
1332630000000 25.03.2012 00:00 40991 0:00:00
1332633600000 25.03.2012 01:00 40991.04167 1:00:00
1332637200000 25.03.2012 03:00 40991.08333 2:00:00
1332640800000 25.03.2012 04:00 40991.125 3:00:00
Note, that excel in formatting the serial date, does not change to DST. So this is not wallclock time.
Long story short:
How should I convert Java time to Excel so that it just works?
I suspect that Excel doesn't really take the time zone into account. I suspect it's really just treating it as a "local time" where every conceivable date/time is valid. (A "local instant" in Joda Time parlance, I believe - although I don't know how widely that's used.)
I suspect there's no way of representing a specific instant in time, and that instead you should:
Take whatever date/time you want to represent as a local time (e.g. "25th March 2012, 3am")
Put that into a Calendar which is set to use UTC
Take the millis from calendar.getTime().getTime()
Subtract the "Excel epoch" value of 1900-01-01T00:00:00Z (again, obtain via a calendar which is set to UTC)
Divide by "millis per day"
Now there's also an oddity with Excel in terms of its handling of dates before March 1st 1900, but hopefully that won't bite you.
Convert floating point "serial date/time" to "mills since 1970/1/1"
Note: daysFrom1900to1970 works for google spreadsheet dates, but might need slight adjustment for excel
int daysFrom1900to1970 =365*70 + 19; // Or maybe =365*70 + 17 But 19 worked.
int millisPerDay = 1000 * 60 * 60 * 24;
long millisSince1970 = (long) ((timeToSend.doubleValue() - daysFrom1900to1970) * millisPerDay );
Calendar dateTimeToSend = Calendar.getInstance();
dateTimeToSend.setTimeZone(TimeZone.getTimeZone("GMT"));
dateTimeToSend.setTimeInMillis(millisSince1970);
System.out.println("timeToSend:"+ new SimpleDateFormat("yyyy.MM.dd HH:mm:ss z").format(dateTimeToSend.getTime()));

Date difference in Java 23 hours day

I have to calculate the difference between to dates, I have found a way but I have this strange result, Am I missing something?
public static void main(String[] args) throws ParseException {
DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
long result = format.parse("2012-03-25 24:00").getTime() - format.parse("2012-03-25 00:00").getTime();
System.out.println("Difference in hours: " + result/(1000*60*60));
result = format.parse("2012-03-26 24:00").getTime() - format.parse("2012-03-26 00:00").getTime();
System.out.println("Difference in hours: " + result/(1000*60*60));
}
This is the result:
Difference in hours: 23
Difference in hours: 24
Thanks for the advices, now I'm using the Joda libray, I have this question, when I calculate the difference in this way:
DateTime begin = new DateTime("2012-03-25T00:00+01:00");
DateTime end = new DateTime("2012-03-26T00:00+01:00");
Hours m = Hours.hoursBetween(begin, end);
If I use this way to calculate the hours I get 24 hours (because the DST is not considered I assume)
What class/calculus should I use in order to get as result the 23 hours considering the DST (I have already tried different ways but I don't get it) the Period class?
Thanks for all the help...
Chances are you happen to have picked a date where daylight saving time changed in that time zone, so the day could really have been only 23 hours long. (March 25th 2012 certainly was the DST change date for Europe, e.g. Europe/London. We don't know what your default time zone is though.)
If you set your date format to use UTC, you shouldn't see this effect. (It's somewhat odd to use 24:00 in a string representation, mind you.) It's not clear what your data is meant to represent though, or what you're trying to measure. You should work out what time zone your data is really meant to be in, if you want to work out how much time actually elapsed between those local times.
(As noted in another answer, Joda Time is a much better API in general - but you still need to know how to use it properly, and when trying to work out the actual elapsed time, you'd still have seen the same results here.)
Must place the library file like explained below.
import java.util.Date;
String dateStart = dateChooserCombo1.getText();
String dateStop =dateChooserCombo2.getText();
//HH converts hour in 24 hours format (0-23), day calculation
SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy");
Date d1 = null;
Date d2 = null;
try {
d1 = format.parse(dateStart);
d2 = format.parse(dateStop);
//in milliseconds
long diff = d2.getTime() - d1.getTime();
long diffSeconds = diff / 1000 % 60;
long diffMinutes = diff / (60 * 1000) % 60;
long diffHours = diff / (60 * 60 * 1000) % 24;
long diffDays = diff / (24 * 60 * 60 * 1000);
//System.out.print(diffDays + " days, ");
jTextField3.setText(""+diffDays);
} catch (Exception e) {
e.printStackTrace();
}

Categories

Resources