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
Related
I have a timestamp in milliseconds and I want to convert this to hours, minutes, seconds but in the below format only:
20hrs 10mins 50sec
I wanted to come up with some better format of reading the data so decided to use above one. I am using DurationFormatUtils class from Apache Commons to give me result in this format HH:mm:ss,SSS as of now:
DurationFormatUtils.formatDuration(durationForStep, "HH:mm:ss,SSS")
Is there any way I can use DurationFormatUtils class only yo get the result in my above format? If not then I think manual work will be the only other option I guess?
I haven't used this, but according to the javadoc shouldn't you be able to just do the following?
DurationFormatUtils.formatDuration(durationForStep, "HH'hrs' mm'mins' ss'sec'");
Please Help me on this.
I had two duration times in string format:
Ex: "10" and "0.30"(actually there are in minutes)
I parsed those values Double and find the find the difference?
But I am getting the output as 9.7 which is wrong
I want it as 9.30.. Please suggest
Thanks in advance!
You can use the Joda-Time Library.Use the Period class from which you can get the difference between two times in terms of hours and minutes.For example refer the second answer to this question
Convert the double into minutes, Here is the link that can help you Convert the string "8:00" into the minutes (integer value).
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
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).
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.