weird issue coverting millisecond to java Date [closed] - java

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
hi im trying to convert a date value in milliseconds granuality into java Date using
new Date( millsecs)
the converted value i get is 3 hours behind what it is supposed to be.
I tried using online tools to conver the millisec value i have and it convert to correct date.
Can some one point out what im missing!!!
thnx

A java.util.Date does not have an hour -- at least not in the way that you probably mean. Try the following, and you'll see that the date simply is a point in time that you can specific as X milliseconds since epoch:
long millisec = System.currentTimeMillis();
Date date = new Date(millisec);
long millisec2 = date.getTime();
If I print this date in New York City's time zone, it will correspond to some hour of the day. If I print the same date in GMT, then the hour will be four larger. You probably are printing the value in such a way that you see the same time zone effect.
Think of a date as a point in time. That's a specific number of milliseconds since epoch. Let's pick 3PM PDT as our point in time. This corresponds to 6PM EDT. In other words, all three of those values (millseconds since epoch, 3PM PDT, and 6PM EDT) occupy the same spot on on a timeline.
Or, here's another explanation. 3PM PDT on some day is NOT the same as 3PM EDT on that day. Let's say 3PM PDT corresponds to M milliseconds since epoch. Then, 3PM EDT = M - 10,800,000 milliseconds (that's the number of milleseconds in three hours).

Related

How to convert epoch time of one timezone to epoch time of another timezone in java? [duplicate]

This question already has answers here:
Convert unix timestamp between different timezones and different DST in Java
(4 answers)
Closed 3 years ago.
I am given epoch time for GMT timezone, i need to convert this time to other timezones based on user time zone provided.
ex- time in GMT = 1551700619
time converted to IST = 1551680819
please help me regarding this.
A few things:
"Epoch time" is a misnomer, and should be abolished from our vocabulary (IMHO). More on this here. What you have is correctly called a "Unix Timestamp".
Timestamps of this form are defined as seconds since the Unix Epoch (1970-01-01 00:00:00 UTC) without accounting for leap seconds.
Because they are based on UTC, they are always in terms of UTC. Presenting one as being in a different time zone is invalid, and can lead to further confusion and corruption as that timestamp is passed around.
The timestamps 1551700619 and 1551680819 are two different points in time, separated by 5 hours and 30 minutes of elapsed time. In other words, if there were events at these two timestamps, and you were in India and on the phone with someone in the United Kingdom, you would both experience the first event, then have to wait 5 hours and 30 minutes, then would both experience the second event.
1551700619 == 2019-03-04T11:56:59Z == 2019-03-04T17:26:59+05:30
1551680819 == 2019-03-04T06:26:59Z == 2019-03-04T11:56:59+05:30

Compare date and see if password is expired(1 year) JAVA [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I will get the date from mySQL in the format of YEAR-MONTH-DAY ex(2013-02-01).
How would i be able to retrieve the Date using ResultSet
and then compare it with todays date to see if its a year old.
ResultSet has getDate() methods that will return a Date object. After that, here's a short way to compare them, if you don't care too much about precision or leap years or such:
Date fromDatabase = ...;
Date now = new Date();
long daysBetween = TimeUnit.DAYS.convert(now.getTime() - fromDatabase.getTime(), TimeUnit.MILLISECONDS);
if (daysBetween > 365) { ... }
If you mean that the date is stored as a string value instead of as a date in the database, you can parse it like new SimpleDateFormat("yyyy-MM-dd").parse("2013-02-01").
When dealing with dates in databases, keep time zones in mind. It can get tricky keeping things straight.

java Retrieve intervals between start date and end date for 30 minutes [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I need to retreive intervals between start date , end date for every 30 minutes.
Ex:If my start date is 2011-12-10-10:00:00 and end date is 2011-12-11-10:00:00
I need to populate the intervals between these two dates in an array.
In JavaScript:
var dates = [],
start = new Date("2011-12-10T10:00:00Z"),
end = new Date("2011-12-11T10:00:00Z"); // make sure the format is parsed by all browsers - or use epoch timestamps
for (var i = new Date(start); i < end; i.setMinutes(i.getMinutes()+30))
dates.push(new Date(i));
You can easily do this with JODA. Use the Interval.withDurationAfterStart() will give you an interval after the start time. Now create a loop that adds the interval to an array, and then gets the next interval using the end time of the previous interval (until you are >= the end instant).
The resulting array will hold your interval list.

How to parse times in Java? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I was wondering if java comes with a built in way to parse times/dates like this one:
5m1w5d3h10m15s
It's 5 months, 1 week, 5 days, 3 hours, 10 minutes, 15 seconds.
Yes, it's called SimpleDateFormat, you just have to define your pattern.
As you didn't (voluntarly?) precise the year, I added one :
DateFormat df = new SimpleDateFormat("M'm'W'w'F'd'H'h'm'm's's'yyyy");
System.out.println(df.parse("5m1w5d3h10m15s"+"2012"));
Of course there are some libraries available (many people redirect to Joda, which is better than the standard and confusing java libraries) but as your question is about "if java comes with a built in way to parse times/dates", the answer is a clear yes.
You probably want to parse this into a time period. rather than into a Date, unless your input looks like "2012y10m8d" etc. Expect to encounter many problems if you try to represent a period of time as a java.util.Date. Trust me, I've been down that path before.
Instead consider using Joda time for time periods. See this question for details: How do I parse a string like "-8y5d" to a Period object in joda time
HumanTime looks interesting too.
I would first reverse the string. Then, I would tokenize the string into 6 numeric, separate parts -- months, weeks, days, hours, minutes, seconds. I'd store each token in an array and treat each element separately: a[0] = seconds, a[1] = minutes, ... a[5] = months. That's the most intuitive way I can think of offhand since I can't recall any library that does this for you. And of course, you didn't specify what you mean by "parse."
I dont know about that specific format... but i suggest you take a look at this
also, it wouldn't be too difficult to build your own parser...
You can refer to this post for ideas:
Java date format - including additional characters
And of course the SimpleDateFormat class for reference.
http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
Using SimpleDateFormatter class will help you convert your string into a date. Using Joda time PeriodFormatter will allow you convert/express a period instead of a date. Eg. you will be able to express and parse something like: 15m1w5d3h10m15s too.

Android: Date/Time Verify [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I created a datepicker and a time picker that apply the date and time in EditText field.
Now I need to do a check, that the date and time inserted are forward in time than the current date. need accept only the date and time that go into the future...
how do?
You need to use a Calendar object to get the current date...
Calendar currentDate = Calendar.getInstance();
Then make a Calendar for the date entered by the user, probably using something like this...
Calendar enteredDate = Calendar.getInstance();
enteredDate.set(Calendar.DATE, dateValue);
enteredDate.set(Calendar.MONTH, monthValue);
enteredDate.set(Calendar.YEAR, yearValue);
Then compare the 2...
boolean isAfterToday = enteredDate.after(currentDate);
If it is true, the entered date is after today's date.
If you need to use time values, specify them in the enteredDate.set() methods - refer to the Java documentation at http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Calendar.html

Categories