How to check if the timestamp is current date's timestamp [duplicate] - java

This question already has answers here:
How to compare two Dates without the time portion?
(33 answers)
Closed 9 years ago.
I have a scheduler that needs to check if the incoming timestamp is current day's timestamp.
The incoming timestamp will be of the format Eg:1384956395.
How to check this in java? Please help. I am not using Joda

The epoch you posted is in seconds. Java uses milliseconds so you have to convert it and then compare the two.
long epochInMillis = epoch * 1000;
Calendar now = Calendar.getInstance();
Calendar timeToCheck = Calendar.getInstance();
timeToCheck.setTimeInMillis(epochInMillis);
if(now.get(Calendar.YEAR) == timeToCheck.get(Calendar.YEAR)) {
if(now.get(Calendar.DAY_OF_YEAR) == timeToCheck.get(Calendar.DAY_OF_YEAR)) {
}
}
You can also change the time zone if you do not want to use the default, in case the input epoch is in a different time zone.

Assuming that your timestamp was created via System.currentTimeMillis() (or any other compatible mechanism), you can do the following:
Create a Calendar instances and set the hour, minute, second and millisecond fields to zero. This is today at 0:00:00,0.
Clone the instance and add 1 day. You'll get tomorrow at 0:00:00,0.
Now check if your timestamp is in the range between today.getTime() (inclusive) and tomorrow.getTime() (exclusive).

Related

Get all milliseconds from LocalTime Java [duplicate]

This question already has an answer here:
How can I return LocalDate.now() in milliseconds?
(1 answer)
Closed 2 years ago.
I need to know how to get LocalTime to milliseconds
LocalTime lunchTime = LocalTime.parse("01:00:00",
DateTimeFormatter.ISO_TIME);
If i am going to execute lunchTime.getMinute() i only get 0 and lunchTime.getHour() i only get 1 as an hour. How to get value in milliseconds?
Try getting nano seconds or seconds and converting to milliseconds (depending on what precision you're dealing with).
lunchTime.toNanoOfDay() / 1e+6
lunchTime.toSecondOfDay() * 1e+3
If you want the millisecond of the day (in other words the count of milliseconds since 00:00), there is a ChronoField enum constant exactly for that:
LocalTime lunchTime = LocalTime.parse("01:00:00");
int millisecondOfDay = lunchTime.get(ChronoField.MILLI_OF_DAY);
System.out.println("Lunch is at " + millisecondOfDay + " milliseconds of the day");
Output:
Lunch is at 3600000 milliseconds of the day
(1 AM is probably a funny time for lunch for some, but for the sake of demonstration and of using your own example.)
The date and time classes of java.time generally have got a get method that accepts a TemporalField argument. LocalTime is no exception to this rule. The most common thing to do when calling get() is to pass a ChronoField constant. The method is very versatile for getting values from the date-time object for which no getXxx method is provided.
Doc link: ChronoField.MILLI_OF_DAY
You can use System.currentTimeMillis(), refer https://currentmillis.com/
Also, please refer https://stackoverflow.com/a/26637209/1270989 to followup on parsing date in your choice of format.
Also, if you trying to get local time in your timezone from an application running in different timezone please follow comment https://stackoverflow.com/a/319398/1270989

File.lastmodified() generates wrong date and month [duplicate]

This question already has answers here:
Why is January month 0 in Java Calendar?
(18 answers)
Why Java Calendar set(int year, int month, int date) not returning correct date? [duplicate]
(3 answers)
Closed 3 years ago.
Im using File.getlastmodified() to get last modifies of files but for some reason (probably UIT) its printing wrong date and month (year is fine)
Im getting last modifed with the above method and saving it to Long
//This should ideally conver the last modified to human radable format
but its returning wrong month adn date
like----> 27/11/2018
shows as---> 28/10/2018
c = Calendar.getInstance();
Long epoch = files[i].lastModified();
epoch+=32400L;
c.setTimeInMillis(epoch); //im converting the milliseconds to human readable formate
int year=c.get(Calendar.YEAR);
int month=c.get(Calendar.MONTH);
int day=c.get(Calendar.DATE);
The value is not incorrect.
You are using the Calendar incorrectly. Checking the documentation you can see the value returned for Month.
Field number for get and set indicating the month. This is a calendar-specific value. The first month of the year in the Gregorian and Julian calendars is JANUARY which is 0; the last depends on the number of months in a year.
Source
Now, for the day, I would suspect the addition of 32 seconds could be the problem. But most probably the timezone. Indeed, the method return a value on GMT.
Note that I have check with a file modified the "2019-04-17 14:52:13" and get the correct result. Also, you can format the Calendar using a SimpleDateFormat instance instead of extracting the value like this.
private static String formatEpoch(long epoch) {
return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(epoch));
}
Or, we can never mention this enough, using a more recent API for date with Instant.
private static String formatEpoch(long epoch) {
DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
return formatter.format(Instant.ofEpochMilli(epoch).atZone(ZoneId.systemDefault()));
}
An instant is a date-time at GMT, so we add the locale timezone before formatting it with a DateTimeFormatter to provide a value like :
2019-04-17T14:52:13.118

How do I create a java.sql.Date object initialized to current time? It keeps getting rounded off to midnight time [duplicate]

This question already has an answer here:
How to save time using java.sql.Date?
(1 answer)
Closed 5 years ago.
I am trying to create a java.sql.Date object initialized to current time but it was being rounded off to midnight time . Can any of you help? I attempted below but it did not work.
new Date(new java.util.Date().getTime())
I can't explain it better than the Javadoc does:
A thin wrapper around a millisecond value that allows JDBC to identify this as an SQL DATE value. A milliseconds value represents the number of milliseconds that have passed since January 1, 1970 00:00:00.000 GMT.
To conform with the definition of SQL DATE, the millisecond values wrapped by a java.sql.Date instance must be 'normalized' by setting the hours, minutes, seconds, and milliseconds to zero in the particular time zone with which the instance is associated.
If you need time information as well, you might use java.sql.Timestamp.

Getting current time of a timezone [duplicate]

This question already has answers here:
How to handle calendar TimeZones using Java?
(9 answers)
Closed 6 years ago.
I am trying to get current time of a particular timeZone but when I write this:
Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("America/Boise"));
System.out.println(calendar.getTime());
it always prints UTC time followed by "UTC" word maybe because on the server timeZone is set to UTC but still it shouldn't happen as I explicitly specified the timezone here.
It returns UTC because the method TimeZone.getTimeZone(String id) returns the specified TimeZone, or the GMT zone if the given ID cannot be understood. In your case, since the given ID cannot b understood, it returns the GMT Zone.

How to compare Date objects ignoring seconds and milliseconds? [duplicate]

This question already has answers here:
Set time to 00:00:00
(14 answers)
Closed 6 years ago.
I am trying to compare Date objects in Java but when I test it, it fails because (I think) of seconds and milliseconds.
Date date = (Date) jSpinner.getValue();
Date now = Calendar.getInstance().getTime();
if(date.before(now))
System.out.println("Error: Date too early");
else
System.out.println("Date is good");
So when I pick from the JSpinner today's date and time (up to minutes) the comparison with now should print the second statement but prints the first one. So how do you tell the date object to set seconds and milliseconds to 0?
You should be able to use Calendar API to zero out the seconds and milliseconds to zero.
https://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html#set(int,%20int)

Categories