I'm trying to retrieve the date only from a java.util.date. I know I can set the time to 0 but I'm searching for a cleaner, more concise way if possible. Does this exist?
Thanks!
Krt_Malta
The cleaner way is to use Joda Time to start with, where there are separate concepts of LocalDate, LocalTime, LocalDateTime, DateTime, Instant etc.
You can translate between java.util.Date and the Joda types, but I would suggest you stick to using Joda for as much of the code as you can, and only translate when you really need to.
Sorry if you'd hoped to avoid an extra library - but Joda is so much better than the built-in API, it's well worth investing in the effort IMO. Note that within the Java types you'll need to determine which time zone you're interested in - java.util.Date defines an instant in time, which may occur in as many as three different dates around the world depending on your time zone.
Another option is the truncate method in the DateUtils library. You put in the date and the field up to which you are interested (in this case CALENDAR.DAY_OF_MONTH). The rest will be set to 0. Effectively:
DateUtils.truncate(Calendar.getInstance(), Calendar.DAY_OF_MONTH).getTime()
did the job.
More info about the method is at: http://commons.apache.org/lang/api-2.4/org/apache/commons/lang/time/DateUtils.html#truncate%28java.lang.Object,%20int%29
Related
Abstract question. Is there "data decorator" bundle for Symfony2? Or how would you solve elegantly data localization/formatting problems?
For example:
float - decimals formatting, monetary data needs monetary formatting
integers - formatting - 1'000'000 vs 1.000.000
dates, times
etc
I know that Symfony1 had some kind decorating going on in templates which would suit this exactly. It wasn't perfect, though and arised other problems.
Anybody knows, is there something? Or what would be the "correct" way for solving this problem?
Your question as it stands is too vague and answers can be considered primarily opinion-based, but you're aware that the native PHP functions do everything that you're asking for, right?
Float, integer formatting: number_format
Date/Time formatting: DateTime class and its subsequent format functions
Twig even uses the equivalent functions to achieve exactly what you're asking for: number_format and the date filters.
This would be the correct standard - and although I have literally no experience with Symfony1, I haven't had any issue deploying a dozen Symfony2 projects using just these two functions to achieve the data decorating that you desire.
I'm designing an API and I want to allow my users to combine a GET parameter with AND operators. What's the best way to do this?
Specifically I have a group_by parameter that gets passed to a Mongo backend. I want to allow users to group by multiple variables.
I can think of two ways:
?group_by=alpha&group_by=beta
or:
?group_by=alpha,beta
Is either one to be preferred? I've consulted a few API design references but no-one seems to have a view on this.
There is no strict preference. The advantage to the first approach is that many frameworks will turn group_by into an array or similar structure for you, whereas in the second approach you need to parse out the values yourself. The second approach is also less verbose, which may be relevant if your query string is particularly large.
You may also want to test with the first approach that the query strings always come into your framework in the order the client sent them. Some frameworks have a bug where that doesn't happen.
Are there any existing implementations in R to parse ISO8601 strings into POSIXt objects? The ISO8601 spec allows date/times to be printed in a variety of (non-overlapping) formats, so one probably needs to do some regular expression magic to detect the format and feed that to strptime.
Doing this properly might actually be quite challenging, however something that detects the most common formats would already be very helpful. I can hardly imagine I am the first one to run into this, but I am having a hard time finding good implementations.
Strictly speaking, you can't. I don't need to know anything about r or cran (or even what they are) to tell you that, because I know ISO 8601 well enough to know that just knowing something is ISO 8601 is not enough to unambiguously know that what is meant by it, especially in the shorter forms.
Find out what profile of ISO 8601 the other party is using. If they don't know what you're talking about, then you will be doing them a favour when you point out what I just said in the paragraph above. As I wrote once elsewhere,
Unfortunately many people think of a particular profile they are familiar with when they hear “ISO 8601”, other people know that using 8601 is a Good Thing but are not familiar with the details of implementation. Hence a spec or requirements document might mention 8601 but not be more explicit than that. In such cases it’s important to seek clarification rather than assume that the format you think of as “ISO 8601” is the correct one to use.
So, tell them "'ISO 8601' is not specific enough, I need to know exactly what you are doing, what your limits on precision are." (And possibly what your policy on dates prior to 1582 and perhaps again prior to 0001 are, your policy on leap-seconds, and a few other things left open but the standard)
Then whatever you're dealing with should be easy enough: Aside from this point of ambiguity, it is a pretty straight-forward standard. It should just be thought of as a standard about defining date formats, more than one that defines a date format.
See .parseISO8601 in the xts package for one implementation. I doubt this will work "out of the box", but it should give you a good idea how to implement your specific needs.
This looks promising: http://cran.r-project.org/web/packages/parsedate
parsedate: Recognize and Parse Dates in Various Formats, Including All
ISO 8601 Formats
Parse dates automatically, without the need of specifying a format.
Currently it includes the git date parser. It can also recognize and
parse all ISO 8601 formats.
t <- strptime("2013-08-20T14:56:37", "%FT%T")
worked well enough for me for most cases. It already fails on fractions of seconds though and does not include solutions to all the problems Jon Hanna mentioned. (And which make working with time data types so unbelievably difficult.)
Is there a natural language parser for date/times in ColdFusion?
There's a (reportedly -- I've not used it) good one for Java called JChronic -- a port of the Ruby Chronic date parser. You could try using it.
It hasn't been updated since 2006, but should still be useful.
I believe parseDateTime() and lsParseDateTime() are the closest to what you're looking for, from the library of built-in ColdFusion functions.
Check out Adobe's LiveDocs for other date/time functions.
Remember that you can also leverage any Java date/time class as well. Pete Freitag has an interesting post with some example code.
Since you are probably using it in a form anyway checkout Datejs, a very nice javascript date library that can parse a lot of dates. It is very easy to incorporate with CF.
I have recently been put in charge of debugging two different programs which will eventually need to share an XML parsing script, at the minimum. One was written with PureMVC, and another was built from scratch. While it made sence, originally, to write the one from scratch (it saved a good deal of memory, but the memory problems have since been resolved).
Porting the non-PureMVC application will take a good deal of time and effort which does not need to be used, but it will make documentation and code-sharing easier. It will also lower the overall learning curve. With that in mind:
1. What should be taken into account when considering whether it is best to move things to one standard?
(On a related note)
Some of the code is a little odd. Because the interpreting App had to convert commands from one syntax to another, it made sense to have an interpreter Object. Because there needed to be communication with the external environment, it made more sense to have one object interact with the environment, and for that to deal with the interpreter exclusively.
Effectively, an anti-Singleton was created. The object would only interface with the interpreter, and that's it. If a member of another class were to try to call one of its public methods, the object would raise an Exception.
There are better ways to accomplish this, but it is definitely a bit odd. There are more standard means of accomplishing the same thing, though they often involve the creation of classes or class files which are extraordinarily large. The only solution which I could find that was standards compliant would involve as much commenting and explanation as is currently required, if not more. Considering this:
2. If some code is quirky, but effective, is it better to change it to make it less quirky, even if it is made a more unwieldy?
In my opinion this type of refactoring is often not considered in schedules and can only be done when there is extra time.
More often than not, the criterion for shipping code is if it works, not necessarily if it's the best possible code solution.
So in answer to your question, I try and refactor when I have time to do so. Priority One still remains to produce a functional piece of code.
Things to take into account:
Does it work as-is?
As Galwegian notes, this is the only criterion in many shops. However, IMO just as important is:
How skilled are the programmers who are going to maintain it? Have they ever encountered nonstandard code? Compare the cost of their time to learn it (including the cost of delayed dot releases) to the cost of your time to refactor it.
If you're maintaining it, then instead consider:
How much time will dealing with the nonstandard code cost you over the intended lifecycle of the codebase (e.g., the time between now and when the whole thing is rewritten)?
That's hard to guess, but consider that many codebases FAR outlive the usefulness envisioned by their original authors. (Y2K anyone?) I've gradually developed a sense of when a refactoring is worthwhile and when it's not, mostly by erring on the side of "not" too often and regretting it later.
Only change it if you need to be making changes anyway. But less quirky is always a good goal. Most of the time spent on a particular piece of software is in maintenance, so if you can do something to make that easier, you'll be reducing the overall time spent on that piece of code. Nonetheless, don't change something if it's working and doesn't need any modifications.
If you have time, now. If you don't have time and it can be avoided, later.