Win a copy of Kotlin for Android App Development this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Devaka Cooray
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

java date related problems  RSS feed

 
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My application demands generating a current date and depending on the condition set an alarm for 24 , 48 or 72 hours.
What I need is to generate a java.sql.Date date which I can pass to a method which does the needed functionality
Here goes my code
long now = System.currentTimeMillis(); //The current time
long 24HourFromNow = now + (24 * 60 * 60 * 1000);
long 48HourFromNow = now + (48 * 60 * 60 * 1000);
long 72HourFromNow = now + (72 * 60 * 60 * 1000);

java.sql.Date dtAlarm = null;

if ( sPenaltyColumn.equals("24") ) {
b24Hour = true;
dtAlarm = new date(24HourFromNow);
penalty = true;
}
else if ( sPenaltyColumn.equals("48") ) {
b48Hour = true;
dtAlarm = new date(48HourFromNow);
penalty = true;
}
else if ( sPenaltyColumn.equals("72") ) {
b72Hour = true;
dtAlarm = new date(72HourFromNow);
penalty = true;
}
markPlayerPenalty(b24Hour, b48Hour, b72Hour, sPlayerID, dtAlarm, con);
When I run this code, I get the following exceptions
Fri Apr 06 22:22:29 GMT+00:00 2001:<E> <ServletContext-Servlets> weblogic.servlet.JSPServlet: Compilation of /opt/devpkg/weblogic_instances/wl9000/classfiles/jsp/_EnforcePlayer.java failed: /opt/devpkg/weblogic_instances/wl9000/classfiles/jsp/_EnforcePlayer.java:296: Invalid expression statement.
long 24HourFromNow = now + (24 * 60 * 60 * 1000); //[ EnforcePlayer.jsp; Line: 242]
^
/opt/devpkg/weblogic_instances/wl9000/classfiles/jsp/_EnforcePlayer.java:296: ';' expected.
long 24HourFromNow = now + (24 * 60 * 60 * 1000); //[ EnforcePlayer.jsp; Line: 242]
^
/opt/devpkg/weblogic_instances/wl9000/classfiles/jsp/_EnforcePlayer.java:296: Invalid character in number.
long 24HourFromNow = now + (24 * 60 * 60 * 1000); //[ EnforcePlayer.jsp; Line: 242]
^
/opt/devpkg/weblogic_instances/wl9000/classfiles/jsp/_EnforcePlayer.java:297: Invalid character in number.
long 48HourFromNow = now + (48 * 60 * 60 * 1000); //[ EnforcePlayer.jsp; Line: 243]
^
/opt/devpkg/weblogic_instances/wl9000/classfiles/jsp/_EnforcePlayer.java:298: Invalid character in number.
long 72HourFromNow = now + (72 * 60 * 60 * 1000); //[ EnforcePlayer.jsp; Line: 244]
^
/opt/devpkg/weblogic_instances/wl9000/classfiles/jsp/_EnforcePlayer.java:304: Invalid character in number.
dtAlarm = new date(24HourFromNow); //[ EnforcePlayer.jsp; Line: 250]
^
/opt/devpkg/weblogic_instances/wl9000/classfiles/jsp/_EnforcePlayer.java:309: Invalid character in number.
dtAlarm = new date(48HourFromNow); //[ EnforcePlayer.jsp; Line: 255]
^
/opt/devpkg/weblogic_instances/wl9000/classfiles/jsp/_EnforcePlayer.java:314: Invalid character in number.
dtAlarm = new date(72HourFromNow); //[ EnforcePlayer.jsp; Line: 260]
^
8 errors

As an alternative, I developed another code to do the same task

java.util.Date today = new java.util.Date();
System.oout.println("today : "+today);
Calendar cal = Calendar.getInstance();
System.oout.println("cal : "+cal);
cal.setTime(today);
System.oout.println("cal : "+cal);
cal.add(Calendar.DATE,1) // Add 1 day or 24 hours
java.util.Date expiration = cal.getTime();
System.oout.println("expiration : "+expiration);
java.sql.Date dtAlarm = ( java.sql.Date ) expiration;
System.oout.println("dtAlarm : "+dtAlarm);
but when I run this code , This time I am getting a null pointer exception
today : Fri Apr 06 19:45:30 GMT+00:00 2001
cal : java.util.GregorianCalendar[time=986586330917,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=java.util.SimpleTimeZone[id=Custom,offset=0,dstSavings=3600000,useDaylight=fa lse,startYear=0,startMode=0,startMonth=0,startDay=0,startDayOfWeek=0,startTime=0,endMode=0,endMonth=0,endDay=0,endDayOfWeek=0,endTime=0],firstDayOfWeek=1,minimalDaysInFirstWeek=1,E RA=1,YEAR=2001,MONTH=3,WEEK_OF_YEAR=14,WEEK_OF_MONTH=1,DAY_OF_MONTH=6,DAY_OF_YEAR=96,DAY_OF_WEEK=6,DAY_OF_WEEK_IN_MONTH=1,AM_PM=1,HOUR=7,HOUR_OF_DAY=19,MINUTE=45,SECOND=30,MILLISEC OND=917,ZONE_OFFSET=0,DST_OFFSET=0]
cal : java.util.GregorianCalendar[time=986586330915,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=java.util.SimpleTimeZone[id=Custom,offset=0,dstSavings=3600000,useDaylight=fa lse,startYear=0,startMode=0,startMonth=0,startDay=0,startDayOfWeek=0,startTime=0,endMode=0,endMonth=0,endDay=0,endDayOfWeek=0,endTime=0],firstDayOfWeek=1,minimalDaysInFirstWeek=1,E RA=1,YEAR=2001,MONTH=3,WEEK_OF_YEAR=14,WEEK_OF_MONTH=1,DAY_OF_MONTH=6,DAY_OF_YEAR=96,DAY_OF_WEEK=6,DAY_OF_WEEK_IN_MONTH=1,AM_PM=1,HOUR=7,HOUR_OF_DAY=19,MINUTE=45,SECOND=30,MILLISEC OND=915,ZONE_OFFSET=0,DST_OFFSET=0]
Exception caught : null
java.lang.NullPointerException
at jsp._EnforcePlayer._jspService(_EnforcePlayer.java:302)
at weblogic.servlet.jsp.JspBase.service(Compiled Code)
at weblogic.servlet.internal.ServletStubImpl.invokeServlet(Compiled Code)
at weblogic.servlet.internal.ServletContextImpl.invokeServlet(Compiled Code)
at weblogic.servlet.JSPServlet.service(Compiled Code)
at javax.servlet.http.HttpServlet.service(Compiled Code)
at weblogic.servlet.internal.ServletStubImpl.invokeServlet(Compiled Code)
at weblogic.servlet.internal.ServletContextImpl.invokeServlet(Compiled Code)
at weblogic.servlet.internal.ServletContextImpl.invokeServlet(Compiled Code)
at weblogic.socket.MuxableSocketHTTP.invokeServlet(Compiled Code)
at weblogic.socket.MuxableSocketHTTP.execute(Compiled Code)
at weblogic.t3.srvr.ExecuteThread.run(Compiled Code)
I am always getting stuck with date related problems in Java , I hope handling date in java would have been made much easier .

Can someone suggest a suitable source on the net for java date related stuff ?
 
Ranch Hand
Posts: 389
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry Guys there was this violation of naming convention for identifiers so I was getting this errors. The identifier name should not start with a number
 
You get good luck from rubbing the belly of a tiny ad:
Programmatically Create PDF Using Free Spire.PDF with Java
https://coderanch.com/wiki/703735/Programmatically-Create-PDF-Free-Spire
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!