• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to get current date and time

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi friends,

I wish to use the current year in my program but unable to figure out a way to do this.There's no direct method in the Java docs.

How can i retrieve the current month and year in my program?

Thanks
 
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
make use of Calendar class in the java.util package
or make use of Date from the java.util package

java.util.Date()
 
Ranch Hand
Posts: 633
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you can also use
 
Sharma Vinit
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hey guys

how can i compare a date with current date. I have to check the joining date of a student which is received from front end as a string.The joining date should not be greater than today's date.

I have tried the following. Its working but i think there must be a better way out





 
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
Please UseCodeTags when you post source code.

That looks a lot more complicated than it needs to be...

You can use class java.text.SimpleDateFormat to parse a string into a Date object. Then you can use the methods after() or before() of class Date to check if the date is before or after another date.
 
pankaj patil
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Look for Date comparison code in java

visit below mention link
http://www.roseindia.net/java/example/java/util/CompareDate.shtml
 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sharma Vinit wrote:
I have tried the following. Its working but i think there must be a better way out



Here's the code.



Hope this is wat you wanted. I;ve returned a boolean, if you really need to send a string please modify.

Pradeep
 
Marshal
Posts: 79471
379
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That doesn't look good. You are either returning true or throwing a checked Exception.
 
Sheriff
Posts: 22787
131
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
DateFormat.parse(String, ParsePosition) returns null when the string is invalid. That can be used:
Not only does this code not throw an exception, it also doesn't create an unnecessary Calendar (and Date from getTime()) object. You can improve this code slightly by checking if the entire String is used in parsing:
If the parse position's index is not directly at the end of the String there is something else in the String as well. This will occur if the passed String is something like "20/05/2010abc". The added check will make the method return false if that's the case.
 
reply
    Bookmark Topic Watch Topic
  • New Topic