Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Compare time (time difference in java)

 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Marshal
Posts: 74777
336
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 74777
336
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.



 
reply
    Bookmark Topic Watch Topic
  • New Topic