Convert to unixtime: same input, different output - java

Maybe this is a recurrent issue, but I need to explain my problem.
I must convert date+hour to Unix time format. I use Apache Commons Lang library, DateUtils class:
Date date = DateUtils.parseDate(strStartDateTime, new String[] { "ddMMyyyyHHmmss" });
Long dateLong = new Long(date.getTime());
Maybe Java Date class is old for this, but it seems to work fine.
But lately, I have detected that the same input, in right expected format, is returning different output in different executions.
I have been looking for a similar issue, and I have read that Java Date is old, joda-time library is recommended, etc.
It would be easy to convert with joda-time, but, why is this happening? Is reliable the code above? It's very likely I'm not taking something into account.
Any help or suggestion would be very appreciated.
Thanks in advance.

Excuse me, but finally I found the error. There was a wrong attribute definition.
It was my programming error; nothing related to time conversion. Thanks

Related

Convert a java list of data into a JSON array of array

Good morning, everyone. Hope you're well. Actually I'd like to convert a list with data into a JSON table of this form:
[[45.21406,5.74749,445,"2019-11-05T15:29:45Z"],
[45.21401,5.74752,470,"2019-11-05T15:29:46Z"],
[45.21397,5.74763,472,"2019-11-05T15:29:47Z"],
[45.21393,5.74789,471,"2019-11-05T15:29:48Z"],
[45.21389,5.74849,473,"2019-11-05T15:29:49Z"]
]
My code is the next :
activity.longitude=result.getLastLocation().getLongitude();
activity.latitude=result.getLastLocation().getLatitude();
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
activity.dateFormated= formatter.format(new Date());
activity.altitude=result.getLastLocation().getAltitude();
activity.data.add(activity.longitude);
activity.data.add(activity.latitude);
activity.data.add(activity.dateFormated);
activity.data.add(activity.altitude);
Thank you very much for your help.
You need to use a JSON library to make this easy and correct. This is a simple one to get started with.
https://github.com/stleary/JSON-java
so this can be done using a host of different libraries but one that could be particularly helpful is Gson. Unfortunately you have not posted your POJO that represented these values so it's difficult to help you further and give an example but this should help.

I am very new to velocity apache script

Present code is am using is like this:
#set($startdate=$datetool.format("yyyymmdd",$fromDate))
#set($enddate=$datetool.format("yyyymmdd",$endDate))
But I dont know how to continue.
I want to find out difference between two dates which are in the format yyyymmdd, and if the difference is greater than one month, I have to divide that into months, so please anyone help to solve this.
I Would suggest you to to use ComparisonDateTool in Velocity to get date difference
$dateComparisonTool.difference($startdate,$enddate).days

Way to detect if String content is a DateTime - RegExp? [duplicate]

This question already has an answer here:
Closed 11 years ago.
Possible Duplicate:
generically parsing String to date
Following situation:
I need to detect if a String contains a DateTime/Timestamp. The problem is that those DateTimes come in various formats and granularity such as:
2011-09-12
12-09-2011
12.09.2011
2011-09-01-14:15
... and many many more variations
I don't need to understand the semantics (e.g. distinct between day or months) I just need to detect let's say 80% of the most common DateTime variations.
My first thought was using RegExp - which I'm far from being familiar with and also I would need to familiarize myselft with all variations in which DateTimes can come.
So my questions:
Does anybody know a canned RegExps to achieve this?
Is there maybe some Java library that could do this task?
Thanks!!
There is another question of same context, hope that link will help you: Dynamic regex for date time formats
you're going to struggle to find a generic match. For the day - month - year section you could possibly use a pattern like (\d{1,2}.){2}\d{4} which would match dates in format dd*mm*yyyy
DateFormat would be a better choice, I think. As John B suggested above, create a list of valid formats and try to match against each one.
Use Java's DateFormat.
You can set up as many formats as you want and iterate through them looking for a match. You will have to catch exceptions for the formats that don't parse and so this solution is not efficient but will work.
Edit per comment:
If you don't want to have exceptions due to performance the you would need to set up a list of regular expressions (one for each format you will support). Find the regex (if any) that matches your input and convert it to a date based on the matching format. What I would suggest would be to match a DateFormat to each regex and let the appropriate DateFormat do the work of parsing once you have identified the appropriate DateFormat. This would reduce the chance of errors in using the groups from the regex to produce the date. Personally, I don't know if this would actually be more efficient than try/catch so I would opt for the more straightforward mechanism (using DateFormat directly).

Parsing Ambiguous Dates in Java

Is there any way in Java to guess the date format when it is not explicitly defined?
For example a user types in 11Mar09 or 11-09-2009 or 11/09/2009 or 11-09 what is the best way of parsing this to a Date object without either a bunch of try catch or raising an error?
I don't think you want to do this, especially based on your examples, but if you must, I think your best bet will be to use something like Apache's DateUtils in commons-lang:
String[] datePatterns = new String[] {
"ddMMMyy", // ex. 11Mar09
"dd-MM-yyyy", // ex. 11-09-2009
"dd/MM/yyyy", // ex. 11/09/2009
"dd-MM" // ex. 11-09
}
Date date = DateUtils.parseDate(stringDate, datePatterns);
Unfortunately dates like the fourth one above will be problematic - is "11/09" September 11th, November 9th, September 2011, November 2009, or something else?
My recommendation is don't. Use a date picker or an explicitely noted format. Guessing will lead to all kinds of problems, easily including, if the date is a critical one, litigation.
If you have to guess, provide some form of feedback that is non-ambiguous, something like a confirmation page that says "Your flight will be booked on the 9th of November, 2009. Is this correct?".
You could have a bunch of regular expressions and cycle through until you find a match. I think you could also have a bunch of DateFormats and cycle through them, catching exceptions on ones that failed.
The first avoids using exceptions for non-exceptional cases, the second is maybe better from a design point of view in that you're using a date parsing framework for what it was designed for. But overall, I don't think either approach is necessarily "best" or "worst" per se-- more a matter of personal preferences/beliefs.
As the domain of possible date-strings are infinite, I don't see how it could be possible to recognize them all. You can however pick a subset for pattern-matching.
You give no clues about your user-interface, but the best approach here would be to help the user input the date. For example with a pop-up calendar or just forcing a predefined format.

Fuzzy date parsing with Java

Are there any libraries for Java that allow you to interpret dates like "Yesterday", "Next Monday", ...
You are looking for Natty. Feel free to fork it and modify its grammar.
Su-time from stanford is your (and my) friend.
A library like Joda-Time is probably as close as you're gonna get. It doesn't convert strings, but offers much more functionality than the Java default Date class.
Here is a utility class that provides some of the functions you're looking for, but again without fuzzy string conversion.

Categories

Resources