Why I am seeing two different values for java.util.Date? - java.util.date

I have a table "Foo" to query programmatically by web application (jsf+spring dao+hibernate). The table has many columns and one of them is date column that its data type is CLOB but data is registered as MM/dd/yyyy format in string. The id column is primary key for the table.
At one point, date value was Feb 16, 2018.
---------------------
| id | date |
---------------------
| 1000 | 02/16/2018 |
---------------------
Today I set it as March 16, 2018 via web application through following code.
Foo foo = tableFooRepository.findById(..); // returns a row in where date is 02/16/2018
SimplDateFormat sdf = new SimpelDateFormat("MM/dd/yyyy");
sdf.setTimeZone(TimeZone.getTimeZone("UTC")); // <-- added originally missed statement
Date lastDate = sdf.parse(foo.getDate());
Date today = <using Calendar.getInstance I have today's date as Fri Mar 16 00:00:00 EDT 2018>
if (lastDate.before(today)) {
foo.setDate(sdf.format(today));
} else {
// do something else
}
Very straightforward process. First time code was executed, date was properly set to "03/16/2018" for the id 1000 in the table.
The problem I having is if I run same code again else statement should be hit but it keeps hitting if statement. When I printed out value lastDate, surprisingly lastDate came out as "Thu Mar 15 20:00:00 EDT 2018".
Logger.info("lastDate :: " + lastDate); // Thu Mar 15 20:00:00 EDT 2018
However if I convert date to string via SimpleDateFormat object, the output is "03/16/2018".
Logger.info("lastDate :: " + sdf.format(lastDate));
I am not sure why lastDate has two different values.
Update:
Answer is found. Plz refer to my last comment. I added originally missing statement into the original water down version of code for the clarification of the question. Had this statement was added, the question would have been clear for others to understand what went wrong.
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
My local jvm is set to "America/New York" timezone however when I convert string MM/dd/yyyy to date object via a formatter, timezone "UTC" was used so that output was different. today was using eastern daylight saving zone but lastDate was using "UTC". Under the hood date value itself was consistent but formatter added different make up on top of it so that it looked different.

Related

Using CONVERT to check for specific day, results in conversion failed if day does not exist

In SQL Server I am writing a query to calculate some time between certain user events, and for this I need to run an aggregate query for set days for an event. An event can run on multiple days, and for the data I am using to validate my query, there is data for Feb 27th, Feb 28th, but the event goes from Feb 25th - March 1st.
I am only using a subset of the data to validate the query, there will be a lot more data which matches more days, or less days.
So I am trying to add an IF check to my query to only run the aggregate if data exists for that specific day, like so:
IF EXISTS(SELECT 1 FROM sourcedata SD
JOIN #dwellTime DT ON SD.badgeid = DT.badgeid
WHERE SD.eventid = 1234
AND CONVERT(date, SD.DateAdded, 110) = '2018-02-29')
But as I say, my #dwellTime data does not have data for 29th, and when it tries to do the convert, no data is there to convert and I receive the
Conversion failed when converting date and/or time from character string.
error.
How can I check for a specific day being in the data?
DateAdded is a datetime column.
I believe the problem is that "2018-02-29" is not a valid date. This year, February had only 28 days. The next leap year is 2020.
So, if you use a valid date for the comparison, you shouldn't have a problem.
If you want to include invalid dates, then you can make the comparison as a string rather than a date. Or, you can use try_convert(date, '2018-02-29'). This will return NULL.

Derive date column based on current date and fixed start date

I have the following two columns:
date (datetime),
month_after_release (int)
These columns are filled monthly as part of an incremental update.
Let's say there was a release on Jan 1, 2015. This means we are currently in the 9th month after release [formula: round off((current date - release date)/30,0)+1 = 9] . So if I am now importing data with a current date (Sep 21, 2015), the two columns should be filled like this:
date = 2015-09-21 00:00:00.000
month_after_release = 9
In October, data which gets imported should then get the following information:
date = 2015-10-21 00:00:00.000
month_after_release = 10
How can I achieve this? One possibility which came to my mind would be to have a separate mapping table, which maps every single date to "month_after_release", but I was wondering if there is a better possibility?
The information when a release was is stored in a separate table, which looks like this:
ReleaseID,
Release_start_date
select releaseDate, DateDiff(MM, releaseDate, getdate()) MonthsSinceRelease
from table t
That's how you calculate it.

How can I determine if the current date is within/outside a range of two dates that don't include a year?

So I have an object (Activity) where I allow users to select start date and an expiry date for an activity.
The user is basically a center/centre that provides activities for the public.
They have the option of making these activities seasonal. So for example an activity could run from Feb 01 to June 22. I use the current date along with a bunch of code to determine if the activity is in season or not.
So for example I check if the current date 271014 is greater than the start date 020114 and less than the expiry date 062214. If it is then it means the date is in range.
The issue:
I've had to make changes, so now the user must select a month and day only. This is fine until they select something like Nov 01 to Feb 28.
These dates would be in the format 1101 and 2802.
Without the year this obviously gives me different results. With the year I would have been able to determine that the expiry date Feb 28 was the year after the start date and not the same year. Then when working out if the current date was within the start date and expiry date I'd get expected results. Without the year the expiry date is now actually not greater than the start date even though it should be.
What I'm currently doing
How do I solve this issue? All I really need is to allow a user to set a start date and an expiry. The activity will show up in a table view only when the current date is in range.
If only an expiry date is selected then the activity will remain active until that expiry date has been reached. Even if I set and expiry date for Feb 28 and we're in November. The same need applies to the start date too.
Would appreciate some insight here.
Thanks for your time.
Here my suggestion (in pseudo-code): Convert all dates to strings in the format "MMdd", in
your example
startDate = "1101" // November 1
expiryDate = "0228" // February 28
currentDate = "1027" // October 27
If startDate <= expiryDate then the range is within the same year and you can check
the current date with
if (startDate <= currentDate && currentDate <= expiryDate) ...
Otherwise the range starts in one year and ends in the next year, and you check with
if (currentDate >= startDate || currentDate <= expiryDate) ...

Check if the current date string is within or outside the range of two other dates

I have 2 date pickers. One is for selecting a start date and the other is for selecting an expiry date. When a selection has been made I convert the date to string format and store the results in a two text fields.
This is how I save to the database:
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "MMMM d" // e.g. September 15
let sDate = dateFormatter.dateFromString(startDateField.text) as NSDate!
let eDate = dateFormatter.dateFromString(expiryDateField.text) as NSDate!
activity["startDate"] = sDate
activity["expiryDate"] = eDate
activity.saveInBackgroundWithBlock({ (success: Bool, error: NSError!) -> Void in
What I intend to do is have a table view display 2 sections: one showing activities that are active, and the other showing activities that are expired/not in date range.
I figured I could do this by taking the current date and checking it was within the start date and expiry date range.
My saved date from above is showing up in parse like this:
The thing is, I don't want to take the year or time into account. The only thing that matters is the month and day. So for example an activity could run once a year between September 15th and December 20th.
I need a way to check that the current date is within those two dates taking the day (e.g. 15th) of the month into account. So today's date is October 19 and an activity would be in range if its start date was September 15 and its expiry date December 20, but would be out of range/operation if the expiry date was October 18 or start date was October 25.
So to summarise:
I will be displaying two sections in a table view. One with active activities and another with non-active out of operation activities.
How do take my stored start date and expiry date and easily check the current date is within their range?
For the occasions when users pick either a start date or expiry date I need to check if the current date is greater/less than the start date (to be able to decide if the activity should show up or not) or if the current date is greater/less than the expiry date (to be able to decide if the activity has passed or not passed its expiry date.
It would be nice if I could roll 1 and 2 into one function and use it in the table view.
Would be interested to learn how the more experienced programmer would achieve this.
Instead of the date format "MMMM d" (e.g. "September 15"), you can convert the dates
to a string with the date format "MMdd" (e.g. "0915"). Then you can do a simple
string comparison between the current date, the start date and the expiry date.
For example,
"0915" < "1019" < "1220"
so October 19 is in the range from September 15 to December 20.

Help ordering a result by date in SQLite

Is there any way in SQLite to ORDER BY a date, and have result be ordered by time rather than alphabetically?
For example:
SELECT * FROM details GROUP BY date;
John | Smith | April 01, 2011
John | Smith | April 03, 2011
John | Smith | April 04, 2011
John | Smith | March 25, 2011
March should come before April.
I'm guessing that the answer here is to store my dates as long timestamps, however I wasn't sure if it could be done more easily with SQLite.
Thanks!
There isn't a built-in DATE type in SQLite (as exists in some other database management systems), but it does have a nice complement of date and time functions: http://www.sqlite.org/lang_datefunc.html
You can use date("April 01, 2011") to get back an ISO-8601 date (e.g., 2011-04-01).
This format has the advantages of both being a readable string and being sortable. 2011-03-25 naturally comes before 2011-04-01 by standard string comparison rules, so there's no special operation required.
So, store your dates in that format, and get that format using the date() function (or other relevant function).
you can convert date to an actual date in order to compare it.
try to add order by julianday(date) to the end of the query
http://www.sqlite.org/cvstrac/wiki?p=DateAndTimeFunctions
date must be a string and not an actual date. You would need to convert date to an actual date. Here is a link to Sqlite date and time functions.
http://www.sqlite.org/lang_datefunc.html
You can do something like this
select * from sometable order by date(thestringDateColumn)
Hope this helps