-2

i am making a university project assignment and i am trying to calculate the days between two dates, but so far i did not achieved anything.

The problem is:

I receive two strings in this format "NNNN@AAAA-MM-DD" and i need to check if the dates are alright and all of that. I already made the methods to do it and they are working. The problem is that i cant figure it out how to see the days between without the use of calendar method or date or any type of those ones.

Thanks ;)

  • Are you not allowed to use libraries? The wording of the question suggests that you're not. – will Nov 19 '14 at 00:18
  • 2
    this has been answered a million times (at least) on stackoverflow – Scary Wombat Nov 19 '14 at 00:19
  • I am not able to use libraries. If so sir tell me the link please ;) – TheCodeMaster Nov 19 '14 at 00:21
  • For [example](http://stackoverflow.com/questions/26858419/subtracting-dates-and-get-difference-in-days-how-to-subtract-them/26858678#26858678), [example](http://stackoverflow.com/questions/12851934/how-to-find-difference-between-two-joda-time-datetimes-in-minutes/12852021#12852021) – MadProgrammer Nov 19 '14 at 00:24
  • So the requirement is that you are not allowed to use Util or libraries, correct? Because everyone here has been using java.util.Date or something fancy <_ – Compass Nov 19 '14 at 00:26
  • Thanks MadProgrammer; I wil check it out ;) – TheCodeMaster Nov 19 '14 at 00:26
  • Yep That is @Compass. That why this is very hard :( ; – TheCodeMaster Nov 19 '14 at 00:31
  • Quick question - are you doing this in Java 8, or an earlier version? Java 8 has some excellent date-related classes that will do this correctly, very easily. Trying to do it correctly in Java 7, without using the Joda library, is rather difficult. – Dawood ibn Kareem Nov 19 '14 at 01:49
  • @TheCodeMaster If your teacher forbids using the java.util.Date and java.time classes and Joda-Time, then obviously **date-time work is not the purpose** of the assignment. The teacher wants you to use the other lessons you've been covering, probably related to string manipulation and converting strings to numbers and checking for things such as month number being >= 1 and <= 12. Of course it's hard, learning always is. Go back over your recent lessons and try to apply them to the assigned problem. – Basil Bourque Nov 19 '14 at 03:00

1 Answers1

-1

you can use this method for convert the string in date:

public Date convertStringToDate(String dateString)
{
    Date date = null;
    Date formatteddate = null;
    DateFormat df = new SimpleDateFormat("NNNN@yyyy-MM-dd");
    try{
        date = df.parse(dateString);
        formatteddate = df.format(date);
    }
    catch ( Exception ex ){
        return null;
    }
    return formatteddate;
}
Mauro Midolo
  • 1,841
  • 3
  • 14
  • 34
  • Thanks you sir. I dont know if i am correct, but what you did there was to get the date (taken from the original string) right? – TheCodeMaster Nov 19 '14 at 00:23