2

I am a novice to Java programming using Netbeans. I have added jCalendar to my GUI to pick a date.

I have entered this line in Events -> "property change" code of jCalendar button,

Date date=jcalendar1.getDate(); 

So that I get the date immediately when it is changed. Am I right?

The purpose: I want to find the difference in milliseconds from the afternoon (12:00 pm) of this date above to NOW (current date and time). There are several programs showing the date difference but all have dates hardcoded and being a newbie i do not know how to replace it with the date that is picked. (also i am confused between the objects Date and Calendar, not able to understand the difference between them). For example, a piece from here:

http://www.java2s.com/Code/Java/Data-type/ReturnsaDatesetjusttoNoontotheclosestpossiblemillisecondoftheday.htm

if (day == null) day = new Date();
  cal.setTime(day);
  cal.set(Calendar.HOUR_OF_DAY, 12);
  cal.set(Calendar.MINUTE,      cal.getMinimum(Calendar.MINUTE));
  cal.set(Calendar.SECOND,      cal.getMinimum(Calendar.SECOND));
  cal.set(Calendar.MILLISECOND, cal.getMinimum(Calendar.MILLISECOND));
  return cal.getTime();

Here day is a Date object. How is cal (a calendar object) linked to it to enter the time. How should the cal object be defined first? How can I use this or anything else in your opinion for my program. A piece of code with detail comments will be more helpful thanks!

JasonMArcher
  • 14,195
  • 22
  • 56
  • 52

1 Answers1

0

Instead of using :

Date day = new Date();

Use:

Calendar cal = Calendar.getInstance();
cal.set (...);
Date date = new Date(cal.getTimeInMillis());

Worth abstracting this stuff out to a DateUtils class or similar, with something like the following:

public static Date create(int year, int month, int day, int hour, int minute, int second) {
    return new Date(getTimeInMillis(year, month, day, hour, minute, second));
}

public static long getTimeInMillis(int year, int month, int day, int hour, int minute, int second, int milliseconds) {
    Calendar cal = Calendar.getInstance();

    cal.clear();
    cal.set(year, month, day, hour, minute, second);
    cal.set(Calendar.MILLISECOND, milliseconds);

    return cal.getTimeInMillis();
}
PaddyC
  • 576
  • 7
  • 14
  • thanks, i have to take the date from GUI, can i use calendar.getInstance(); for that instead of Date date=jcalendar1.getDate(); as stated in my question. – Rajesh Acharya May 01 '12 at 10:04
  • The jcalendar API -http://www.toedter.com/en/jcalendar/api/com/toedter/calendar/JCalendar.html - indicates that a java.util.Date object will be returned from the getDate() method. The getTime() method on java.util.Date gives you a representation of that date in milliseconds. Use that with the code in my response to get the difference in milliseconds between GUI and today at midday. – PaddyC May 01 '12 at 10:19
  • Sorry I haven't commented too often here, I hit enter a little quickly, but does my comment above answer your question? – PaddyC May 01 '12 at 10:22
  • Thanks paddyC, I have to get the date from the UI and then set the time to noon (1200hrs). I am trying this but it seems there is an error which i dont get: Date date.jDateChooser2.getDate(); cal.setTime(date); cal.set(...); The error is at cal.setTime(date); line.(??) In your suggestion, i think, you are not using the date given by user, but setting it yourself. Also, can i use calendar object to get the date chosen on the GUI using jCalendar – Rajesh Acharya May 03 '12 at 08:53
  • Could solve the problem using calendar object and getCalendar() instead of using date object and getDate(); Thanks for your time special thanks to paddy. – Rajesh Acharya May 04 '12 at 10:03