I am trying to calculate the age of the person based on the date of birth and doing some logic if its over 18 years. I had my code written and it was working fine, but I stumbled upon a code I found online and I am not getting one condition in that. The code is:
public class AgeValidation {
public static void main(String[] args) {
getAge("29-12-1999");
}
private static void getAge(String dob1) {
SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy");
Date dob;
try {
dob = format.parse(dob1);
Calendar dob2 = Calendar.getInstance();
dob2.setTime(dob);
Calendar today = Calendar.getInstance();
int age = today.get(Calendar.YEAR) - dob2.get(Calendar.YEAR);
if(dob2.after(today)) {
System.out.println("Future date not allowed");
System.exit(0);
}
if (today.get(Calendar.MONTH) < dob2.get(Calendar.MONTH)) {
System.out.println("First if condition");
age--;
} else if (today.get(Calendar.MONTH) == dob2.get(Calendar.MONTH)
&& today.get(Calendar.DAY_OF_MONTH) < dob2.get(Calendar.DAY_OF_MONTH)) {
System.out.println("else if condition");
age--;
}
if (age < 18) {
System.out.println(age);
System.out.println("Underage");
} else {
System.out.println(age);
System.out.println("18 years");
//Some logic
}
} catch (ParseException e) {
e.printStackTrace();
}
}
}
Need addressing on below points:
I have added a condition if DOB year is after Current year it should not proceed.
if(dob2.after(today)) {
System.out.println("Future date not allowed");
System.exit(0);
}
Is it correct to use System.exit(0); or is there some better approach to stop further execution.
In the code that I found online I saw a condition as
` if (today.get(Calendar.MONTH) < dob2.get(Calendar.MONTH))`
I am not getting in which scenario will this be executed.
Is there any use case in which this code will not work (I cannot think of any)
java.time
You should not (as in not) want to use the long outdated classes SimpleDateFormat, Date and Calendar. Especially the first is notoriously troublesome, but we have better replacements for all of them in java.time, the modern Java date and time API also known as JSR-310.
And even more so because the modern API has a method for counting years between two dates built-in. Not only is it easier to write the code, more importantly it is easier to read and understand, and you can be more sure of the correctness.
private static final DateTimeFormatter dateFormatter
= DateTimeFormatter.ofPattern("dd-MM-uuuu");
private static void getAge(String dob1) {
LocalDate dob = LocalDate.parse(dob1, dateFormatter);
LocalDate today = LocalDate.now(ZoneId.of("Asia/Dushanbe"));
if (dob.isAfter(today)) {
System.out.println("Future date not allowed");
} else {
int age = (int) ChronoUnit.YEARS.between(dob, today);
if (age < 18) {
System.out.println(age);
System.out.println("Underage");
} else {
System.out.println(age);
System.out.println("18 years");
//Some logic
}
}
}
With your example date of "29-12-1999" the above method prints:
17
Underage
Since it is never the same date in all time zones, please substitute your desired time zone instead of Asia/Dushanbe.
between() returns a long. In this case we can safely cast it to an int because LocalDate only handles years in the range -999 999 999 through 999 999 999, so the difference in years will never exceed the capacity of int.
Your questions
Use of System.exit(0); is generally questionable, though at times necessary. In my code I have avoided it using an if-else construct. Another option would be return;. I guess this would more give you what you wanted in case there were two calls to getAge() after each other. Yet another option is throwing an IllegalArgumentException, that would leave for the caller to decide to catch it or not.
The line you are quoting will not be executed when running your code here in December. Imagine running your code next January, then today’s month will be January and dob2’s month will still be December, so since January is before December, the code will be executed. Which will also be necessary for your method to calculate the correct age.
The code seems convoluted, as Jim Garrison said, but appears to be correct (not even with the outdated API needed it be this complex). I have not spotted a case that would not be handled correctly.
Question: Can I use the modern API with my Java version?
If using at least Java 6, you can.
In Java 8 and later the new API comes built-in.
In Java 6 and 7 get the ThreeTen Backport, the backport of the new classes (ThreeTen for JSR 310).
On Android, use the Android edition of ThreeTen Backport. It’s called ThreeTenABP, and there’s a thorough explanation in this question: How to use ThreeTenABP in Android Project.
For learning to use java.time, see the Oracle tutorial or find other resoureces on the net.
I am giving you a solution which doesn't take care of leap year logic. But you can build upon this approach.
public static void main(String[] args) throws Exception{
String date ="11_10_1991";
SimpleDateFormat sdf = new SimpleDateFormat("dd_MM_yyyy");
Date birthDate = sdf.parse(date);
long ageInMillis = System.currentTimeMillis() - birthDate.getTime();
long years = ageInMillis /(365 * 24*60*60*1000l);
long leftover = ageInMillis %(365 * 24*60*60*1000l);
long days = leftover/(24*60*60*1000l);
System.out.println(years);
System.out.println(days);
}
Related
I'm trying to make a app which includes telling the time of next Thursday. App crashes every time i open that class.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_authorised);
button.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
nextThursday();
}
});
}
void nextThursday(){
String nextThursday = getNext(DayOfWeek.THURSDAY).format(DateTimeFormatter.ofPattern("MMM, dd yyyy", Locale.ENGLISH));
nextThurs.setText(nextThursday);
}
public static LocalDate getNext(DayOfWeek dayOfWeek) {
// get the reference day for the word "next" (that is the current day)
LocalDate today = LocalDate.now();
// start with tomorrow
LocalDate next = today.plusDays(1);
// as long as the desired day of week is not reached
while (next.getDayOfWeek() != dayOfWeek) {
// add one day and try again
next = next.plusDays(1);
}
// then return the result
return next;
}
}
Is anyone able to help?
This answer uses java.time, which is the datetime API to be used since the Joda Time project stopped further development.
It basically uses an algorithm that may be realizable in Joda Time, too, but I don't know exactly if and how, so I show you a way in java.time.
Define a method that returns the date of the next given day of week:
public static LocalDate getNext(DayOfWeek dayOfWeek) {
// get the reference day for the word "next" (that is the current day)
LocalDate today = LocalDate.now();
// start with tomorrow
LocalDate next = today.plusDays(1);
// as long as the desired day of week is not reached
while (next.getDayOfWeek() != dayOfWeek) {
// add one day and try again
next = next.plusDays(1);
}
// then return the result
return next;
}
and use it in a main() just to print it out:
public static void main(String[] args) {
System.out.println("Next Thursday is " +
getNext(DayOfWeek.THURSDAY)
.format(DateTimeFormatter.ofPattern("MMM, dd yyyy", Locale.ENGLISH)));
}
which results in the output when executed Friday, 15th of May 2020:
Next Thursday is May, 21 2020
Of course, the format is just an example and can easily be adjusted according to your needs.
It's quite simple using a predefined TemporalAdjuster:
LocalDate date = LocalDate.now()
.with(TemporalAdjusters.next(DayOfWeek.THURSDAY));
This is natively supported since Java 8 or Android API level 26. To target previous API levels, use the ThreeTen Android Backport.
Joda Time is in maintenance mode, and they're suggesting that you use java.time instead.
Deleted all code in that activity. Still crashed. Error was not in the code. Something else got messed up instead.
I have an array of cell[] containing all classes for a student ID, each cell has two variables relevant to this context:
int day and int startTime.
I also have two given values for the currentDay and currentTime, both in the same format as cell's day and startTime
I want to make a loop that finds the cell element that contains the next class.
I've tried looping through the cell array and selecting the closest day that contains at least one class, with some success, and I imagine that I would need to make another array that contains all the classes for that day then do the same logic to them. I just can't figure out how.
public Cell getNextClass(int studentID, Context context){
DBHelper dbHelper = new DBHelper(context);
Cell[] cell = dbHelper.queryCellData(studentID);
Date currentTime = Calendar.getInstance().getTime();
int currentDay = getCurrentDay(currentTime.toString());
Log.d(TAG, "currentDay: " + currentDay);
DateFormat dateFormat = new SimpleDateFormat("hh a");
String formattedDate= dateFormat.format(currentTime);
int currentHour = getCurrentHour(formattedDate);
//TODO Find next class given currentDay and currentHour
Cell nextClass = cell[0];
for (int i = 0; i < cell.length; i++) {
if (cell[i].getDay() >= currentDay && cell[i].getDay() <= nextClass.getDay()){
//?????
}
}
return nextClass;
}
}
In this example, cell[0] has one class on hour 12, and is displayed by default because the loop doesn't change anything. However at time of posting in NZ the values would be:
currentDay = 2
currentHour = 21
As shown in this screenshot of my timetable: i.imgur.com/X6HzR6y.png
The next class will be tomorrow, day 3, hour 8.
I recommend that you use java.time classes for the fields of your Cell class, not int:
public class Cell {
DayOfWeek day;
LocalTime startTime;
// Constructor, getters, toString, etc.
}
Also if you can, fit your class with a method that given a day of week and time calculates how long it is until the class occurs next time:
public Duration getTimeUntilNext(DayOfWeek currentDay, LocalTime currentTime) {
int daysUntil = day.getValue() - currentDay.getValue();
Duration time = Duration.ofDays(daysUntil).plus(Duration.between(currentTime, startTime));
if (time.isNegative()) { // over for this week
// Calculate time until next week’s occurrence
time = time.plusDays(7);
assert ! time.isNegative() : time;
}
return time;
}
If you cannot add this method to the Cell class, you may declare it a static method somewhere else and just add the cell as an extra parameter.
Now the rest is not so bad:
Cell[] classes = new Cell[] {
new Cell(DayOfWeek.THURSDAY, LocalTime.of(12, 0)),
new Cell(DayOfWeek.MONDAY, LocalTime.of(14, 0)),
new Cell(DayOfWeek.THURSDAY, LocalTime.of(10, 0)),
new Cell(DayOfWeek.FRIDAY, LocalTime.of(9, 0)),
new Cell(DayOfWeek.THURSDAY, LocalTime.of(6, 0))
};
ZonedDateTime now = ZonedDateTime.now(ZoneId.of("Pacific/Auckland"));
final DayOfWeek currentDay = now.getDayOfWeek();
final LocalTime currentTime = now.toLocalTime();
Comparator<Cell> comparatorByTimeUntilNext = new Comparator<Cell>() {
#Override
public int compare(Cell c1, Cell c2) {
return c1.getTimeUntilNext(currentDay, currentTime)
.compareTo(c2.getTimeUntilNext(currentDay, currentTime));
}
};
Cell nextClass = Collections.min(Arrays.asList(classes), comparatorByTimeUntilNext);
System.out.println("Next class from " + now + " is " + nextClass);
When I ran this just now, it output:
Next class from 2019-06-20T06:49:23.188+12:00[Pacific/Auckland] is THURSDAY at 10:00
Even if you stick with int fields in your class, you should still be able to use the idea. Then getTimeUntilNext may either still return a Duration or an int denoting the number of hours. I recommend the former, of course.
It seems to me that your code was formatting the current date and time into two different strings and parsing each of them back to get the day and time. That’s certainly the detour. Also as I already said in a comment, I recommend you avoid Date, Calendar, DateFormat and SimpleDateFormat since they are all poorly designed and long outdated.
Question: Can I use java.time on Android?
Yes, java.time works nicely on older and newer Android devices. It just requires at least Java 6.
In Java 8 and later and on newer Android devices (from API level 26) the modern API comes built-in.
In Java 6 and 7 get the ThreeTen Backport, the backport of the modern classes (ThreeTen for JSR 310; see the links at the bottom).
On (older) Android use the Android edition of ThreeTen Backport. It’s called ThreeTenABP. And make sure you import the date and time classes from org.threeten.bp with subpackages.
Links
Oracle tutorial: Date Time explaining how to use java.time.
Java Specification Request (JSR) 310, where java.time was first described.
ThreeTen Backport project, the backport of java.time to Java 6 and 7 (ThreeTen for JSR-310).
ThreeTenABP, Android edition of ThreeTen Backport
Question: How to use ThreeTenABP in Android Project, with a very thorough explanation.
Here is a good entry point for you: https://docs.oracle.com/javase/tutorial/datetime/iso/index.html
I do understand that you don't want to change your code. But since your application deals with dates and time it should use the build in api's the java language gives you. Just try to introduce it a controlled and testable way. The String class is also massive, but I bet you use that one, and not your own implementation.
Thank you for your reference, #Jocke however, I found my own way of doing it (poorly), so for posterity and the sake of any poor student who coded themselves into the same situation as me, I just added a bunch of messy edge cases for how time works in looped week scenarios like timetables. I will be using default libraries when possible in the future. Don't you worry. Here's the final method that I used.
public Cell getNextClass(int studentID, Context context){
DBHelper dbHelper = new DBHelper(context);
Cell[] cell = dbHelper.queryCellData(studentID);
Date currentTime = Calendar.getInstance().getTime();
int currentDay = getCurrentDay(currentTime.toString());
Log.d(TAG, "currentDay: " + currentDay);
DateFormat dateFormat = new SimpleDateFormat("hh a");
String formattedDate= dateFormat.format(currentTime);
int currentHour = getCurrentHour(formattedDate);
ArrayList<Cell> classesInDay = new ArrayList<>();
if (currentHour >= 17){
currentDay++; //If the current hour is past the end of the day, add 1 to the day so that tomorrow is scanned.
}
if (currentDay > 4){
currentDay = 0; //If the current day is greater than 4 (the weekend) set day to monday.
}
for (int i = 0; i < cell.length; i++) {
if (cell[i].getDay() == currentDay){
Log.d(TAG, "getNextClass: cell " + i +" has a class today");
classesInDay.add(cell[i]);
}
}
Cell nextClass = classesInDay.get(0);
Log.d(TAG, "getNextClass: "+classesInDay.size());
//If today's classes are over, then search tomorrow.
if (classesInDay.size() == 1 && classesInDay.get(0).getStartTime() < currentHour){
classesInDay.clear();
currentDay++;
for (int i = 0; i < cell.length; i++) {
if (currentDay > 4){
currentDay = 0; //If the current day is greater than 4 (the weekend) set day to monday.
}
if (cell[i].getDay() == currentDay){
Log.d(TAG, "getNextClass: cell " + i +" has a class today");
classesInDay.add(cell[i]);
}
}
nextClass = classesInDay.get(0); //ReApply default class for new day
}
for (int i = 1; i < (classesInDay.size()) ; i++) {
int diff1 = classesInDay.get(i).getStartTime() - currentHour-1; //Minus one to ensure that the next class if found, not the current one.
int diff2 = nextClass.getStartTime() - currentHour-1;
Log.d(TAG, "diff1: "+diff1+" diff2: "+diff2);
if (diff1 < 0){ //This means that the Test cell is before the current hour
}else if(diff2 < 0){ //This means that the current choice is before the current hour
nextClass = classesInDay.get(i);
}else if (diff1 < diff2){ //This means that the test cell is closer to the current hour than the current choice
nextClass = classesInDay.get(i);
}else if (diff2 < diff1){ //This means that the current choice is closer to the current hour than the test cell
}
}
return nextClass;
}
This question already has answers here:
Java - Check if array contains 3 consecutive dates
(4 answers)
Closed 5 years ago.
I have an array of unique dates from each time the user completes a task. I want to check if the dates within the array are consecutive from and including todays date.
If the array contains dates: "2017/6/2, 2017/6/3, 2017/6/4, 2017/6/5" then based on today's date being 2017/6/5 the function would return 4 as there are 4 consecutive dates from and including today.
If the array contains dates "2017/6/2, 2017/6/3, 2017/6/4" then it would return 0 as the array does not include today's date. Otherwise the count would be broken upon a non consecutive date.
List<Date> dateList = new ArrayList<Date>();
int count = 0;
Date todayDate = new Date();
for (int i=0; i<dateList.size(); i++){
// Check if dates within the array are consecutive from todayDate, if so then increment count by 1.
}
If you're using Java 8, consider using the new java.time API. It's easier, less bugged and less error-prone than the old APIs.
If you're using Java <= 7, you can use the ThreeTen Backport, a great backport for Java 8's new date/time classes. And for Android, there's the ThreeTenABP (more on how to use it here).
Although you can also use JodaTime, it's being discontinued and replaced by the new APIs, do I don't recommend start a new project with joda. Even in joda's website it says: "Note that Joda-Time is considered to be a largely “finished” project. No major enhancements are planned. If using Java SE 8, please migrate to java.time (JSR-310).".
As you want to compare just the date (day/month/year), and not the time (hour/minute/second), the best choice is to use the LocalDate class. For java 8, this class is in java.time package, and in ThreeTen Backport, the package is org.threeten.bp. But the classes and methods names are the same.
The code would be like this:
public int count(List<LocalDate> dateList, LocalDate today) {
if (!dateList.contains(today)) { // today is not in the list, return 0
return 0;
}
int count = 0;
LocalDate prev = dateList.get(0); // get first date from list
for (int i = 1; i < dateList.size(); i++) {
LocalDate next = dateList.get(i);
if (prev.plusDays(1).equals(next)) {
// difference between dates is one day
count++;
} else {
// difference between dates is not 1
// Do what? return 0? throw exception?
}
prev = next;
}
return count + 1; // didn't count the first element, adding 1
}
Testing this method:
List<LocalDate> dateList = new ArrayList<>();
dateList.add(LocalDate.of(2017, 6, 2));
dateList.add(LocalDate.of(2017, 6, 3));
dateList.add(LocalDate.of(2017, 6, 4));
dateList.add(LocalDate.of(2017, 6, 5));
LocalDate today = LocalDate.now();
System.out.println(count(dateList, today)); // 4
Another test (when today is not in the list)
List<LocalDate> dateList = new ArrayList<>();
dateList.add(LocalDate.of(2017, 6, 2));
dateList.add(LocalDate.of(2017, 6, 3));
dateList.add(LocalDate.of(2017, 6, 4));
LocalDate today = LocalDate.now();
System.out.println(count(dateList, today)); // 0
Notes:
As it wasn't specified what to do when the days are not consecutive (return 0 or throw exception), I left this part commented. But it should be straightforward to add this to the code
If you want to convert java.util.Date to LocalDate, you can do as follows (using the code of this answer, full explanation is in this link in case you have any questions):
public LocalDate convert(Date date) {
return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
}
// if your Date has no toInstant method, try this:
public LocalDate convert(Date date) {
return Instant.ofEpochMilli(date.getTime()).atZone(ZoneId.systemDefault()).toLocalDate();
}
I understood that you want to check for consecutive days (so, a 1-day difference between the dates). But if you want to check if the previous date is before the next (no matter how many days), you can change the if (prev.plusDays(1).equals(next)) to if (prev.isBefore(next))
I'm not sure if that's the case, but if you want, you can also parse a String directly to a LocalDate (so you don't need to create lots of Date objects), using a DateTimeFormatter:
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/M/d");
LocalDate d = LocalDate.parse("2017/6/2", formatter); // 2017-06-02
There are a lot of ways to write it more clear:
Use new Date API;
Use libraries;
But, in such case, with usage of old Date classes, I would do that in such a way:
public static void main(String[] args) {
long millisInDay = TimeUnit.DAYS.toMillis(1);
List<Date> dates = Arrays.asList(new Date("2017/6/2"), new Date("2017/6/3"), new Date("2017/6/4"), new Date("2017/6/5"));
System.out.println(getSequentialNumber(millisInDay, dates));
}
private static int getSequentialNumber(long millisInDay, List<Date> dates) {
int count = 0;
Date now = setMidnight(Calendar.getInstance().getTime());
for (int i = dates.size() - 1; i >= 0; i--) {
Date date = setMidnight(dates.get(i));
if (date.getTime() == now.getTime()) {
count++;
}
now.setTime(now.getTime() - millisInDay);
}
return count;
}
private static Date setMidnight(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.MILLISECOND, 0);
calendar.set(Calendar.HOUR, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.HOUR_OF_DAY, 0);
return calendar.getTime();
}
If I understand the requirement correctly, you have an array of Date objects, ordered by date, and guaranteed not to have two Date objects for the same day, but possibly with gaps between the days. Your goal is to return the length of the maximum sub-array that contains only consecutive days and also includes the current day, or to return 0 if there is no such sub-array. The current day may fall anywhere inside that sub-array, not necessarily at the beginning or end.
It's not clear if you need to support crossing year boundaries, but I'll assume so. I also assume that all the Date objects in the list are for the same time zone which is also the time zone for the device on which you are running. If that's not the case, you should refer to this answer for more information on testing whether two Date objects refer to the same day.
It's fairly simple to do this if you work with Calendar objects instead of Date objects. You don't need any third-party libraries, as both Date and Calendar are parts of the standard Android API. I suggest doing this in two phases: first search for the current date in the array and then scan in both directions for either a gap in the dates or an array boundary. Then just count how far you could go in each direction.
public int getDateSpanCount(List<Date> dateList) {
final int n = dateList.size();
final Calendar today = Calendar.getInstance();
final Calendar other = Calendar.getInstance();
int count = 0;
// First search for today in the date array
int posToday = -1;
for (int i=0; i<n; i++) {
other.setTime(dateList.get(i));
if (areSameDay(today, other)) {
posToday = i;
break;
}
}
// If today is in the list, count the size of the sub-array containing today
if (posToday >= 0) {
count++; // count today, at least
final Calendar probe = Calendar.getInstance();
// scan backwards from position of today's date
for (int prevPos = posToday - 1; prevPos >= 0; prevPos--) {
final Date prev = dateList.get(prevPos);
probe.setTime(prev);
other.add(Calendar.DAY_OF_YEAR, -1);
if (areSameDay(probe, other)) {
count++;
other.setTime(prev);
} else {
break;
}
}
// reset the other time
other.setTime(today.getTime());
// scan forward from position of today's date
for (int nextPos = posToday + 1; nextPos < n; nextPos++) {
final Date next = dateList.get(nextPos);
probe.setTime(next);
other.add(Calendar.DAY_OF_YEAR, 1);
if (areSameDay(probe, other)) {
count++;
other.setTime(next);
} else {
break;
}
}
}
return count;
}
/** Test whether two Calendar objects are set to the same day */
private static boolean areSameDay(Calendar c1, Calendar c2) {
// see discussion above if dates may not all be for the local time zone
return c1.get(Calendar.YEAR) == c2.get(Calendar.YEAR) &&
c1.get(Calendar.DAY_OF_YEAR) == c2.get(Calendar.DAY_OF_YEAR);
}
I am working on a project that will run many thousands of comparisons between dates to see if they are in the same month, and I am wondering what the most efficient way of doing it would be.
This isn't exactly what my code looks like, but here's the gist:
List<Date> dates = getABunchOfDates();
Calendar month = Calendar.getInstance();
for(int i = 0; i < numMonths; i++)
{
for(Date date : dates)
{
if(sameMonth(month, date)
.. doSomething
}
month.add(Calendar.MONTH, -1);
}
Creating a new Calendar object for every date seems like a pretty hefty overhead when this comparison will happen thousands of times, soI kind of want to cheat a bit and use the deprecated method Date.getMonth() and Date.getYear()
public static boolean sameMonth(Calendar month, Date date)
{
return month.get(Calendar.YEAR) == date.getYear() && month.get(Calendar.MONTH) == date.getMonth();
}
I'm pretty close to just using this method, since it seems to be the fastest, but is there a faster way? And is this a foolish way, since the Date methods are deprecated? Note: This project will always run with Java 7
I can't comment on whether to use the deprecated methods, but if you choose not to there's no need to instantiate a new Calendar for every Date you check. Just use one other Calendar and call setTime(date) before the check (or one Calendar for every thread if you parallelize it).
As a side note, I do have to agree with ChristopheD's comment that this is something worthy of a database.
I think you can define a static DateFormat to extract the month and year from Date and use both objects as date only.
public static DateFormat formatter= new SimpleDateForm("MMyyyy");
public static boolean sameMonth(Date date1, Date date2)
{
return formatter.format(date1).equals(formatter.format(date2));
}
I have a list of objects called Activity:
class Activity {
public Date activityDate;
public double amount;
}
I want to iterate through List, group them by date and return a new list . Here's what I currently do:
private List<Activity> groupToList(List<Activity> activityList) {
SimpleDateFormatter sdf = new SimpleDateFormatter("YYYY-MM-DD");
Map<String,Activity> groupMap = new HashMap<String,Activity>();
for (Activity a in activityList) {
String key = sdf.format(a.getActivityDate());
Activity group = groupMap.get(key);
if (group == null) {
group = new Activity();
groupMap.add(key, group);
}
group.setAmount(group.getAmount() + a.getAmount());
}
return new ArrayList<Activity>(groupMap.values());
}
Is it a WTF to use the DateFormatter in this way?
I'm using the DateFormatter because each activityDate could have time information.
I would just use the date object itself as the key. If it it bothers you because the date object is mutable, then use its toString() value. No reason to go making formats.
If the issue is that you want to normalize the date by removing the time component, it would be much better to do that withing the Activity object and remove the time component. If the issue is still further that there are potential time zone issues, I would use JodaTime, but there is no object in the JDK currently that represents a pure date without time, so going with a string isn't outrageous, but it should be hidden behind a method in the Activity object and the fact that it is a date formatted string without a time component should be an implementation detail.
java.util.Date is a quite poor abstraction for your need; it is IMO fair to stick to strings if nothing better is around, HOWEVER Joda-time provides a good datatype for you: DateMidnight or alternatively LocalDate if Activity is strictly timezome-independant.
other than that, the code looks good to me, you might be able to shorten it a bit using an implementation of Multimap, to avoid messy null-checking code. to be honest, it doesn't get much shorter than your solution:
public List<Activity> groupedByDate(List<Activity> input) {
//group by day
final Multimap<DateMidnight, Activity> activityByDay
= Multimaps.index(input, new Function<Activity, DateMidnight>() {
#Override
public DateMidnight apply(Activity from) {
return new DateMidnight(from.activityDate);
}
});
//for each day, sum up amount
List<Activity> ret = Lists.newArrayList();
for (DateMidnight day : activityByDay.keySet()) {
Activity ins = new Activity();
ins.activityDate = day.toDate();
for (Activity activity : activityByDay.get(day)) {
ins.amount+=activity.amount;
}
}
return ret;
}
Why not simply create a HashMap<Date, Activity>() instead of the roundabout way with Strings?
Sorry, I didn't answer the question. The answer is: yes, unless I am an idiot ;)
You could do this using the Date as the key if you used a TreeMap and provided a Comparator that only compared the year, month and day and not the time.
As already mentioned the best solution is to represent your date with day precission. If this is not possible joda is nice library.
If you can ignore daylight saving time then grouping by date can be accomplished much easier. A unix time day is 86 400 s long. The timestamp does ignore leap seconds. (Your timer stops for one second or the leap second is distributed in some way.) All date values were day is equal are the same day:
int msPerDay = 86400 * 1000;
long day = new Date().getTime() / msPerDay
One minor point is to adjust the timezone. For my timezone CET (UTC/GMT +1 hour) the GMT day starts one our later:
new GregorianCalendar(2009, 10, 1, 1, 0).getTime().getTime() / msPerDay) ==
new GregorianCalendar(2009, 10, 2, 0, 59).getTime().getTime() / msPerDay) ==
new Date().getTime() / msPerDay
If the daylight saving time is significant the best way is to use joda. The rules are just to complicated and locale specific to implement.