Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
The program that I am developing needs to determine if a given date is older than a week. I have come up with the following code:
((new Date()).getTime() - date.getTime() > 7 * 24 * 60 * 60 * 1000)
Tell me if the code is correct. Help me write correct code if it's incorrect. Also, if you have better ways to code, let me know.
Coming from the same country, I think I understand why she posted this question. The reason is quite complicated and and you may not understand unless you have a working understanding of the culture. Anyway, saying the following code is a better way would suffice.
System.currentTimeMillis() - date.getTime() > 7 * 24 * 60 * 60 * 1000
As stated in the comments, you should have run the code and have a test for it.
Concerning correctness, it is not correct for all cases, since for example it does not respect summer/winter time. So if you have an hour shift in the middle of your time range you get a different result as without. Additionally, Date has no timezone information, so in many cases you experience trouble when comparing dates created by different machines.
That is one of the reasons that you should almost always use APIs for date/time calculations. Since Java 8 is not public yet, you probably have to live with the ugly existing classes or use one of the available open source APIs in this regard - but, don't do it by yourself.
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I'd like to print a date in Java containing only the month and the day (no year). It's gotta be localized, so that it prints "May 2015" is the US, and "2015년 5월" in Korea (notice that in US the years comes after the month, but in Korea it comes before; so patterns like "MMMM yyyy" don't work).
I don't care to use any specific libraries. I'm using JDK 7, and I've tried working with Joda-Time to no success.
The Java locale framework supports internationalization of some common forms of date / time, numbers, currency and so on. For date / time values the set of predefined formats is described in the Java Tutorial: Using Predefined Formats.
Unfortunately, "year and month" is not one of the formats.
This leaves you with only one option. Write your code to support multiple formats, and select the most appropriate one based on the locale you are rendering / displaying for. By hand.
Speculation.
I can't tell you why Java SE doesn't support this, but I suspect that it is a combination of two things:
Very few applications would need this. The Java API designers are parsimonious ... and tend to avoid "bloating" the APIs with support for features that are merely convenient for tiny minority of applications.
The conventions for displaying year / month are fluid. For instance, you say that the convention in the US is "May 2015", but in some contexts it would be acceptable or even preferable to display as "2015, May". In the face of this fluidity, I can understand why the Sun / Oracle engineers might not want to buy into the debate.
Having said that, it is possible to submit an RFE. (If you are prepared to wait years for a positive outcome.)
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a program which involves with a bunch of huge numbers (I have to put them into bignumbers type). The time complexity is unexpectedly huhge too. So, I was wondering, do these two factors have a connections? Any comments are greatly appreciated.
Do they have a connection to each other? Probably not.
You can have a large complexity algorithm working on small numbers (such as calculating the set of all sets for ten thousand numbers all in the range 0..30000) and you can have very efficient algorithms working on large numbers (such as simply adding up ten thousand BigInteger variables).
However, they'll both probably have a cascading effect on the time it takes your program to run. Large numbers will add a bit, a high-complexity algorithm will add a bit more I say 'add' but the effect is likely to be multiplicative, much worse - for example, using an inefficient algorithm may may your code take 30% longer, and the use of BigInteger may add 30% to that, giving you a 69% overall hit:
t * 1.3 * 1.3 = 1.69t
Sorry for the general answer but, without more specifics in the question, a general answer is the best you'll probably get. In any case, I believe (or at least hope) it answers the question you asked.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I am looking into finding algorithm within the area of clustering or machine learning which will facilitate or creating a typical data reading for a group of readings. The issue is that it must facilitate time series data; thus some traditional (k-means) techniques are not as useful.
Can anyone recommend places to look or particular algorithms that would provide a typical reading and relatively simple to implement (in Java), manipulate and understand?
As an idea. Try to convert all data types into time, then you will have vectors of the same type (time), then any clustering strategy will work fine.
By converting to time I actually mean that any measurement or data type we know about has a time in its nature. Time is not a 4-th dimension, as many think! Time is actually 0-dimension. Even a point of no physical dimensions which may not exist in space, exists in time.
Distance, weight, temperature, pressure, directions, speed... all measures we do can be converted into certain functions of time.
I have tried this approach on several projects and it payed back with really nice solutions.
Hope, this might help you here as well.
For most machine learning problems in Java, weka usually works pretty well.
See, for example: http://facweb.cs.depaul.edu/mobasher/classes/ect584/weka/k-means.html
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I have to determine if a package can be delivered based on rules as follows...
If packageWeight > 70 and below 100
AND
If destinationState is in the special list of allowed state {MA, PA, NY, NJ, LA,....}
AND
contentType is any of the following {ACD, FDY, PRZ, QUO, ..... }
AND
CarrierName is any of the following {FXD, USPS....}
if the above conditions are true then we can deliver the package
and in addition there are 3 flags with 15x16x8 possibilities?
what is the best way to avoid a if else cluster ?
hash map?
any other suggestions ?
You can break down different group of conditions in different functions. For. e.g. Define a function isCarrierSupported that returns true if carrier is one of FXD, USPS etc. Similarly you can define multiple functions like isWeightCorrect etc. Then later in main program, you can just call these functions and get final result.
This does not avoid ifs and elses but neatly organizes the program to make it more readable.
What are looking for a is a rules engine, then just use one that already exists.
This isn't a trivial domain to be trying to create something from scratch. Just like you should not be writing your own caching system or your own ORM or your own threading framework.
Don't make the mistake of rolling your own it will end in tears.
Since you are using Java, go look at Drools. Save yourself and your company a bunch of money and use something that is already built for you.
HashMap would work or You could convert whole flag-state into 11-bit mask (15-4 bits, 16 - 4 bits, 8 - 3 bits, or leave more bits for further growth) and use true/false array by 11-bit index.
The downside is - You'll anyway have to code same if/else logic to fill the map/array or to define a constant describing all deliver (or don't-deliver) states.
P.S. switch/case looks more appropriate then if/else here...
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
If I have a date supplied to a system written in Java, is there any way of telling if it is GMT or BST?
For formatting, you should really use a DateFormat implementation (e.g. SimpleDateFormat). That will let you specify the time zone (and output format).
Ultimately, you need to post code so we know what a "date supplied to a system" looks like.
It can always be identified. (EDIT: Unless you just got a string, like you did.)
There's something like this if you're interested in a TimeZone class.
Use those proper DateFormat Java Docs. Answer your own questions, then ask the ones you can't answer. That's what SO is for.
The Date class is timezone dependent.
One google search prevents a duplicate like this.