• Post Reply Bookmark Topic Watch Topic
  • New Topic

Compare time (time difference in java)  RSS feed

 
Guy Belpa
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hey All,


i'm developing a website using Java..
for a specific page i want to limit access to that page by TIME of the day..
simply i want to give a error massgae after 6pm if user try to access that page..
so i want to check isCurrentTimeBefore 6 pm...

code i tried is...

java.sql.Time time = new Time(System.currentTimeMillis());
System.out.println("\nisBefore6() currentTimeMillis\n"+time);


java.sql.Time time0 = new Time(06,00,00);
System.out.println("\ntime0=\n"+time0.getTime());



System.out.println("\ncompare "+time2.before(time));


but it is NOT working for my requirement..
Any help will be appriciated.
Thanks in advance.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16047
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are you using class java.sql.Time for this? Use class java.util.Date or java.util.Calendar instead. For example:
 
Guy Belpa
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for quick reply... thanku very much , sir.
i will try it...

just few minitues b4 i tried below ...


i understand it is NOT a proper way for doing it..
It seems your suggestion is much better if it works!
 
Campbell Ritchie
Marshal
Posts: 56221
171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't write abbreviations like "b4". Read this FAQ.
 
Guy Belpa
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Helo Gurus,

caN anyone of you expert go through both code snaps suggested here can
suggest the better one?

Thank in advance!
 
Campbell Ritchie
Marshal
Posts: 56221
171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You will save object creation time if you use the time methods of the System class Note I said time methods; I know of two, so you should consider carefully which is more appropriate for your purposes.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16047
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guy Belpa wrote:
java.sql.Time time0 = new Time(06,00,00);

One more thing: Don't prefix integer literals in your source code with a 0 (like 06, 00, 00 in your source code).

Integer literals that are prefixed with a 0 are interpreted by the Java compiler as octal contants, not decimal constants. Try this, you'll see you'll get a compiler error:

Do you understand why that will give you a compiler error?
 
Guy Belpa
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear experts,

Can you go through both code snaps and suggest the better one?

Thank in advance!

Jesper Young wrote:


Vs.



 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!