Calculate Days Between Dates "JAVA" [duplicate] - java

This question already has answers here:
Calculating the difference between two Java date instances
(45 answers)
Closed 8 years ago.
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 ;)

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;
}

Related

Set timezone to IST, but I still get a wrong-date error [duplicate]

This question already has answers here:
Java date format conversion - getting wrong month
(8 answers)
Closed 5 years ago.
I want to take current time irrespective to the system date. I am using Glassfish server and derby in netbeans. I tried the code below for getting current date according to IST:
DateFormat df = new SimpleDateFormat("yyyymmdd");
df.setTimeZone(TimeZone.getTimeZone("ist"));
String gmtTime = df.format(new java.util.Date().getTime());
java.util.Date parsed = null;
try {
parsed = (java.util.Date) df.parse(gmtTime);
} catch (ParseException ex) {
Logger.getLogger(EmployeePanel.class.getName()).log(Level.SEVERE, null, ex);
}
java.sql.Date date = new java.sql.Date(parsed.getTime());
but I am getting 2017-01-08 instead of 2017-08-08.
m is minute not month (M). The right pattern is:
SimpleDateFormat("yyyyMMdd");
The pattern definition is case sensitive!
For more informations see the javadoc of SimpleDateFormat

How to validate dates in two different formats [duplicate]

This question already has answers here:
How do I convert the date from one format to another date object in another format without using any deprecated classes?
(10 answers)
Closed 5 years ago.
I am doing some junit test. I am getting the date as response in the form of date as below :
2017-08-14 00:00:00.0 +0:00
The data which is present in oracle DB is
14-AUG-17 12.00.00.000000000 AM +00:00
I want to use an assert like this but it is failing. can anyone help to make sure that both expected and actual matches.
Assert.assertEquals("14-08-2017", 2017-08-14 00:00:00.0 +0:00);
You can create two Date objects and for the assertion.
SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
try {
Date parsedDate1 = formatter.parse("14-08-2017");
System.out.println(parsedDate1);
SimpleDateFormat formatter2 = new SimpleDateFormat("yyyy-MM-dd");
Date parsedDate2 = formatter2.parse("2017-08-1 00:00:00.0 +0:00");
Assert.assertEquals(parsedDate1, parsedDate2);
} catch (ParseException e1) {
}
You can use SimpleDateFormat to make string from date.
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
Assert.assertEquals("14-08-2017", sdf.format(<your date instance>));

Finding difference between dates in java [duplicate]

This question already has answers here:
Calculating difference in dates in Java
(7 answers)
Closed 6 years ago.
I am new to java. I am having slight confusion regarding date arithmetic in java. I have following scenario, where I want to find differences between two dates :-
java.util.Date objDt1 = getDate1FromSrc1(); // I am obtaining it from src1
java.util.Date objDt2 = getDate2FromOtherSrc(); // I am getting dt2 by other way.
Now, I want to find difference between two dates and the output must be other date object.
So, I have written the following code :-
Calendar objCal1 = Calendar.getInstance();
Calendar objCal2 = Calendar.getInstance();
objCal1.setTime(objDt1);
objCal2.setTime(objDt2);
objCal1.add(Calendar.DAY_OF_MONTH, -objCal2.get(Calendar.DAY_OF_MONTH));
objCal1.add(Calendar.MONTH, -objCal2.get(Calendar.MONTH));
objCal1.add(Calendar.YEAR, -objCal2.get(Calendar.YEAR));
objCal1.add(Calendar.HOUR_OF_DAY, -objCal2.get(Calendar.HOUR_OF_DAY));
objCal1.add(Calendar.MINUTE, -objCal2.get(Calendar.MINUTE));
objCal1.add(Calendar.SECOND, -objCal2.get(Calendar.SECOND));
java.util.Date objDiff = objCal1.getTime();
But, I am getting some wierd results. Eg. If objDt1 is "02/22/2016 09:00:00" and objDt2 is "02/22/2016 11:00:00", then I am expecting objDiff to be "02:00:00" as output, which I am not getting.
Can you suggest me what's wrong I am doing here and what's the right way to approach this problem ?
Thanks in advance.
In order to find the difference between two dates you can simply convert it to long as follows:
java.util.Date objDt1 = getDate1FromSrc1();
java.util.Date objDt2 = getDate2FromOtherSrc();
long date1 = objDt1.getTime();
long date2 = objDt2.getTime();
System.out.println("Difference (In Seconds): " + (date2 - date1)/1000);
If you want this difference to be in HH:mm:ss format then you can do something like this:
public static void main (String[] args) throws Exception
{
SimpleDateFormat sf = new SimpleDateFormat("HH:mm:ss");
java.util.Date objDt1 = getDate1FromSrc1();
java.util.Date objDt2 = getDate2FromOtherSrc();
long date1 = objDt1.getTime();
long date2 = objDt2.getTime();
System.out.println("Difference: " + sf.format(new Date((date2 - date1)/1000)));
}

how do i get the difference between two dates in days in java [duplicate]

This question already has answers here:
Difference in days between two dates in Java?
(19 answers)
Closed 8 years ago.
i am trying to get the difference between two dates. one of the dates was parsed from a string(dateEmployd) and the other date is the current date (currentDate). This is what i did to get the dates...
public static Date getActiveService(String DtEmplydString){
SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd");//formater for parsed String date
Date dateEmployd, currentDate,periodDifference = null;
try{
dateEmployd = ft.parse(DtEmplydString);
currentDate = new Date();
}catch(ParseException e){
JOptionPane.showMessageDialog(null, e);
}
return periodDifference;
}
Now, i am meant to return periodDifference but i dont know how i would find the difference betweent the two dates (dateEmployd and currentDate) and display it in years or days or a combination of both.
please guys much help is needed. thanks in advance...
Take a look at the jodatime library. They have functions like
DateTime dateEmployd = DateTimeFormat.forPattern("yyyy-MM-dd").parse(DtEmplydString);
Years.yearsBetween(dateEmployd, DateTime.now())
The same for Days.daysbetween, Seconds, Hours etc.
long diff = date2.getTime() - date1.getTime();
You could try out the Java.Calendar for date functions because Date is deprecated .
Here is an example with Cdate comparators
Calendar start = Calendar.getInstance();
start.setTime(from ( your initial Date object here ) );
Calendar end = Calendar.getInstance();
end.setTime(new Date());
int actualDays = start.compareTo(end);

compare date and time with String and perform some logic [duplicate]

This question already has answers here:
Comparing date strings in Java [duplicate]
(6 answers)
Closed 8 years ago.
I am getting date and time which is stored in a String.Below is the date and time which is stored in string.
String userDateTime = "26-Aug-2014 09.00.00 AM";
I have to compare current date and time with the one which is stored in String and if date or time is past date or time or equals to current date and time, it has to perform some logic. Please suggest how can i compare current date and time which is stored in String with the system date and time.
I can use java.util.Date but not sure how can i compare with string format.Please suggest.
First convert the string to date using below way:
SimpleDateFormat ss = new SimpleDateFormat("dd-MMM-yyyy HH.mm.ss a");
String dateInString = "26-Aug-2014 09.00.00 AM";
try {
Date date = ss.parse(dateInString);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
hen you can compare the dates
You need to parse it first, then compare as a Date (or Joda DateTime) - not as String. There is no way you can achieve that with comparing Strings unless you implement some complicated logic using regex.
You can do something like this:
String string = "26-Aug-2014 09.00.00 AM";
Date date = new SimpleDateFormat("d-MMMM-yyyy", Locale.ENGLISH).parse(string);
See this answer from BalusC:
Java string to date conversion
Then you can compare them with date.compareTo(otherDate)

Categories

Resources