• 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

Doubt in how to covert date Object to Calendar ObJect

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey,
I am trying to convert a Date Object to Calendar Object it seems it goes right but the results seems to be not right.They tend to give false results Can anyone one help me.

The Code is given below.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Read the javadocs of the SimpleDateFormat class about what "Y" means.
 
Greenhorn
Posts: 28
1
Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Lokesh,

Few things wrong in your code:

1. Change pattern from dd-MM-YYYY to dd-MM-yyyy.
Y - Represents week year
y - represents year
2. To get value from a calendar, don't use like c.DATE. Instead you should be using get function:
System.out.println(c.get(Calendar.DATE));
System.out.println(c.get(Calendar.MONTH));
System.out.println(c.get(Calendar.YEAR));

Check here

Try with these changes and it should work.

 
lokesh koppaka
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Ulf Dittmer, I change the format to "dd-MM-yyyy", but the answers appears to be false again.The System.out.println() lines are not printing the correct date,Month,Year.
 
Anuj Sharma R
Greenhorn
Posts: 28
1
Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Lokesh,

You should use Calendar class get function to retrieve the field values. Like c.get(Calendar.DATE).
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Anuj Sharma R is right. c.DATE is not the date of the Calendar object c, but the constant Calendar.DATE (similar for MONTH and YEAR).
 
lokesh koppaka
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Anuj and Rob, got it!!! Thanks....
 
Rob Spoor
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Probably better to upgrade to Java8 and use the new Dates API. The old API works, but it is a triumph of user‑unfriendly design.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic