Spring boot Between two date Method Not Working - java

Am using Spring boot and Spring data Jpa with JpaReposity
am getting issue SpringBoot Jpa Query Method
JPA Repo Method
List<BatteryEntity> findByCurrenDateAfterAndCurrenDateBefore(Date start, Date end);
This Mathod is working fine but issue when i pass 2 parameter as any date its working but when i pass new Date() as 2 parameter its working fine
And Service Layer Class method
System.out.println("Start Date_______________" + startDate);
System.out.println("Start Date_______________" + endDate);
System.out.println(new Date());
String startDate = "28/11/19";
String endDate = "07/12/19";
SimpleDateFormat formatter5 = new SimpleDateFormat("dd/MM/yyyy");
Date date1 = formatter5.parse(startDate);
Date date2 = formatter5.parse(endDate);
if(date2.after(new Date())){
System.out.println("Date1 is after Date2");
}
// before() will return true if and only if date1 is before date2
if(date2.before(new Date())){
System.out.println("Date1 is before Date2");
}
List<BatteryEntity> blist = batteryRepo.findByCurrenDateAfterAndCurrenDateBefore(date1, date2);
System.out.println(blist.size());
And output am getting
Start Date_______________27/11/19
Start Date_______________07/12/19
Sat Nov 30 22:53:16 IST 2019
Date1 is before Date2
0
But when i pass 2end Date as new Date() its give me Proper Value
List<BatteryEntity> blist = batteryRepo.findByCurrenDateAfterAndCurrenDateBefore(date1, new Date());
System.out.println(blist.size());
and output
Start Date_______________27/11/19
Start Date_______________07/12/19
Sat Nov 30 22:57:44 IST 2019
Date1 is before Date2
3807

Replace
SimpleDateFormat formatter5 = new SimpleDateFormat("dd/MM/yyyy");
with
SimpleDateFormat formatter5 = new SimpleDateFormat("dd/MM/yy");
Please check the following code and its output to understand the reason:
String startDate = "28/11/19";
String endDate = "07/12/19";
SimpleDateFormat formatter5 = new SimpleDateFormat("dd/MM/yyyy");
Date date1 = formatter5.parse(startDate);
Date date2 = formatter5.parse(endDate);
System.out.println(date1);
System.out.println(date2);
formatter5 = new SimpleDateFormat("dd/MM/yy");
date1 = formatter5.parse(startDate);
date2 = formatter5.parse(endDate);
System.out.println(date1);
System.out.println(date2);
System.out.println(new Date());
Output:
Tue Nov 28 00:00:00 GMT 19
Thu Dec 07 00:00:00 GMT 19
Thu Nov 28 00:00:00 GMT 2019
Sat Dec 07 00:00:00 GMT 2019
Sat Nov 30 18:00:48 GMT 2019

Related

How to select only date with JXDatepicker

When I select date in SQL it is returned as:
Wed Jan 31 00:00:00 GMT+01:00 2018
But I need only the Date part, that is Jan 31 2018. How can I do this?
There may be a reason to use org.jdesktop.swingx.JXDatePicker but rather of using JXDatePicker, I will simply show GMT parsing using java.util.Date.
Try this source code:
System.out.println(new Date());
//will show your Date along with your local TimeZone
//result for me is : Sat Jan 13 08:47:59 IST 2018
//First changing local pacific time zone to GMT+01 level explicitly,
//otherwise it will show results as your local time zone by default.
TimeZone.setDefault(TimeZone.getTimeZone("GMT+01"));
String existingDateValue = "Wed Jan 31 00:00:00 GMT+01:00 2018";
DateFormat gmtFormat =
new SimpleDateFormat("E MMM dd hh:mm:ss zzz yyyy");
try {
//try to parse and validate existing date
Date validatedExistingDate = gmtFormat.parse(existingDateValue);
System.out.println(validatedExistingDate);
//parsed validated date : Wed Jan 31 00:00:00 GMT+01:00 2018
DateFormat newFormat = new SimpleDateFormat("MMM d, yyyy");
System.out.println(newFormat.format(validatedExistingDate));
//required Date is in GMT format : Jan 31, 2018
} catch (ParseException pex) {
pex.printStackTrace();
} finally {
System.out.println("Current TimeZone : " + new Date());
//now, reverting to my local TimeZone
TimeZone.setDefault(TimeZone.getTimeZone("IST"));
System.out.println("Current TimeZone : " + new Date());
}

Conversion from GMT to CST showing 7 hrs difference

I have taken this date "2016-04-26 12:00:00”, and converted to GMT and CST epochs, using the function below. I got the dates below. Not sure I am doing anything wrong here.
1461672000000 UTC ——> Tue, 26 Apr 2016 12:00:00 GMT
1461690000000 CST —> Tue, 26 Apr 2016 17:00:00 GMT
Code:
long epoch = 0;
String str = "2016-04-26 12:00:00";
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
df.setTimeZone(TimeZone.getTimeZone("CST")); //This GMT or CST
Date datenew = df.parse(str); //parsethe date
epoch = datenew.getTime(); //get the epoch time
As eluded to by Erickson in the comments, your expectations seem inverted from the implementation; when you set the TimeZone in the DateFormat, using the DateFormat.parse() method results in the string it's parsing as if it is coming from that TimeZone (and converts it to the local time). So the results you notice are exactly expected.
To fix this, use the DateFormat.format() method instead:
public static void main(String[] args) {
String dateStr = "2016-04-26 12:00:00";
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
df.setTimeZone(TimeZone.getTimeZone("GMT"));
Date gmtDate = null;
try {
gmtDate = df.parse(dateStr);
}
catch (ParseException e) {
e.printStackTrace();
}
System.out.println("GMT TIME = " + df.format(gmtDate));
df.setTimeZone(TimeZone.getTimeZone("CST"));
System.out.println("CST TIME = " + df.format(gmtDate));
}
Output:
GMT TIME = 2016-04-26 12:00:00
CST TIME = 2016-04-26 07:00:00

SimpleDateFormat is not giving right time?

http://ideone.com/T5wSRV this is the link to below code
SimpleDateFormat dateFormatIST = new SimpleDateFormat("yyyy-MMM-dd HH:mm:ss");
dateFormatIST.setTimeZone(TimeZone.getTimeZone("IST"));
//Time in IST
Date date=dateFormatIST.parse( dateFormatIST.format(new Date()) );
System.out.println(date);
this is not giving correct IST time where as code below is working fine . why?
http://ideone.com/9KSaZx this is the link to below code which is giving the desired output.Help me understand the behavior.
SimpleDateFormat dateFormatIST = new SimpleDateFormat("yyyy-MMM-dd HH:mm:ss");
dateFormatIST.setTimeZone(TimeZone.getTimeZone("IST"));
//Local time zone
SimpleDateFormat dateFormatLocal = new SimpleDateFormat("yyyy-MMM-dd HH:mm:ss");
//Time in IST
Date date=dateFormatLocal.parse( dateFormatIST.format(new Date()) );
System.out.println(date);
The behaviour is logical. The point is that there is no information of time-zone is a Date object. A Date object contains Universal Time.
And when you format then parse the formatted string, you still have the same date:
I commented the code with the results:
SimpleDateFormat dateFormatIST = new SimpleDateFormat("yyyy-MMM-dd HH:mm:ss");
dateFormatIST.setTimeZone(TimeZone.getTimeZone("IST"));
//Local time zone
SimpleDateFormat dateFormatLocal = new SimpleDateFormat("yyyy-MMM-dd HH:mm:ss");
//Time in IST
Date d = new Date();
System.out.println(d);
// Mon Mar 16 16:57:19 CET 2015
=> now in my TZ (CET)
System.out.println(dateFormatIST.format(d));
// 2015-Mar-16 21:27:19
=> now in IST TZ
System.out.println(dateFormatLocal.format(d));
// 2015-Mar-16 16:57:19
=> now in my TZ (CET)
Date dateIST = dateFormatIST.parse(dateFormatIST.format(d));
System.out.println(dateIST);
// Mon Mar 16 16:57:19 CET 2015
=> The dateIST object contains still "now", and the format is default local which is CET
Date dateLoc = dateFormatLocal.parse(dateFormatLocal.format(d));
System.out.println(dateLoc);
// Mon Mar 16 16:57:19 CET 2015
=> same thing as above
Date dateLocIST = dateFormatLocal.parse(dateFormatIST.format(d));
System.out.println(dateLocIST);
// Mon Mar 16 21:27:19 CET 2015
=> dateFormatIST.format(d) gives "2015-Mar-16 21:27:19", and dateFormatLocal.parse() will interpret it like a local (CET for me) date. The result is then "Mon Mar 16 21:27:19 CET 2015".
If you need to translate dates between different time-zone, you certainly need to go for the Calendar class.

Iterate between two dates including start date?

Sorry for the apology for asking repeated question..
public static void main(String[] args)throws Exception {
GregorianCalendar gcal = new GregorianCalendar();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM");
Date start = sdf.parse("2010.01");
Date end = sdf.parse("2010.04");
gcal.setTime(start);
while (gcal.getTime().before(end)) {
gcal.add(Calendar.MONTH, 1);
Date d = gcal.getTime();
System.out.println(d);
}
}
In the above code prints between dates exactly but i need to print start date also..
above code output is
Mon Feb 01 00:00:00 IST 2010
Mon Mar 01 00:00:00 IST 2010
Thu Apr 01 00:00:00 IST 2010
But i need also start date on my output..
please help me to get this
Thanks in advance..
In my opinion this is the nicest way:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM");
Date start = sdf.parse("2010.01");
Date end = sdf.parse("2010.04");
GregorianCalendar gcal = new GregorianCalendar();
gcal.setTime(start);
while (!gcal.getTime().after(end)) {
Date d = gcal.getTime();
System.out.println(d);
gcal.add(Calendar.MONTH, 1);
}
Output:
Fri Jan 01 00:00:00 WST 2010
Mon Feb 01 00:00:00 WST 2010
Mon Mar 01 00:00:00 WST 2010
Thu Apr 01 00:00:00 WST 2010
All we do is print the date before incrementing it, then we repeat if the date is not after the end date.
The other option is to duplicate the printing code before the while (yuck) or to use a do...while (also yuck).
You could use a do-while loop, but you would need to alter the end date depending on whether you want to include it or not.
The example below includes all months between 01st or 04th inclusive...
try {
Calendar gcal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM");
Date start = sdf.parse("2010.01");
Date end = sdf.parse("2010.05");
gcal.setTime(start);
do {
Date d = gcal.getTime();
System.out.println(d);
gcal.add(Calendar.MONTH, 1);
} while (gcal.getTime().before(end));
} catch (ParseException exp) {
exp.printStackTrace();
}
Example output...
Fri Jan 01 00:00:00 EST 2010
Mon Feb 01 00:00:00 EST 2010
Mon Mar 01 00:00:00 EST 2010
Thu Apr 01 00:00:00 EST 2010
Equally, you could simply use your current code and change the start Date to one month earlier...
Date start = sdf.parse("2009.12");
//...
Updated
Another approach, based on the previous ideas...
Simply move the start date back a month before you start the loop...
Calendar gcal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM");
Date start = sdf.parse("2010.01");
Date end = sdf.parse("2010.04");
gcal.setTime(start);
// Move the month back by one before we start...
gcal.add(Calendar.MONTH, -1);
while (gcal.getTime().before(end)) {
gcal.add(Calendar.MONTH, 1);
Date d = gcal.getTime();
System.out.println(d);
}
This is reasonably simple and allows you to supply variable dates without needing to care to remember that you need to start one month earlier....
You can just output it right before the start of the loop:
System.out.println(gcal.getTime());
while (gcal.getTime().before(end)) {
gcal.add(Calendar.MONTH, 1);
Date d = gcal.getTime();
System.out.println(d);
}
then just do this:
Date lNow = gcal.getTime();
System.out.println(lNow);
while (gcal.getTime().before(end)) {
gcal.add(Calendar.MONTH, 1);
Date d = gcal.getTime();
System.out.println(d);
}
Since Java 8, you can use the new java.time API:
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("uuuu.MM");
YearMonth date = YearMonth.parse("2010.01", formatter);
YearMonth end = YearMonth.parse("2010.05", formatter);
while (!date.isAfter(end)) {
System.out.println(date.format(formatter));
date = date.plusMonths(1);
}

How to get Hours from a date using java

What is the date format to get only hours in 12-hours format from this time
Thu Oct 20 13:12:00 GMT+02:00 2011
edit:
using this code
Date eventDate = tempAppointments.get(i).mStartDate
System.out.println(eventDate.toString());
// date pattern
DateFormat df = new SimpleDateFormat("hh:'00' a");//output : Wed Nov 09 11:00:00 GMT+02:00 2011
// get the start date with new format (pattern)
String hours = df.format(tempAppointments.get(i).mStartDate.getDay());
System.out.print(hours);//output: 02:00 AM
return hours as
02:00 AM
but for the given time. it must be 02:00 PM . why ?
I'm not sure why you are passing date.getDay() (which is deprecated, by the way) into the formatter if you want the hour part.
Try this:-
Date date = new Date();
System.out.println("Date: " + date);
DateFormat df = new SimpleDateFormat("hh:'00' a");
String hour = df.format(date);
System.out.println("Hour: " + hour);
The output:
Date: Sat Nov 19 17:57:05 CST 2011
Hour: 05:00 PM
Date fecha = new Date();
System.out.println("Fecha "+fecha);
DateFormat formato = new SimpleDateFormat("hh:mm:ss");
String hora = formato.format(fecha);
System.out.println("Son las "+hora);

Categories

Resources