Quantcast

Maximum PC

It is currently Wed Sep 17, 2014 2:50 am

All times are UTC - 8 hours




Post new topic Reply to topic  [ 9 posts ] 
Author Message
 Post subject: Java Help
PostPosted: Wed Jul 20, 2005 9:02 pm 
Thunderbird
Thunderbird
User avatar

Joined: Wed Jul 07, 2004 1:13 pm
Posts: 817
Location: Missouri
I am working on a project I have been wanting to do for a little while. I know there are lots of applications out there, but I wanted to do it myself. I am wanting to create a alarm clock program in Java.

I am using JDK 1.05_03 and have reviewed the Calendar class I am using in this program. I am working slowing on this since I have a little less than one month off from work (without pay). I have it running as a console program first then want to modify it to work in JFrame.

I have displayed the entire instance of the Calendar I have created to check the values of the different variable, but the YEAR variable is not working properly for me. Here is the code I have.

Code:
/*
   created:    7/19/2005
   Modified: 7/20/2005
*/

import java.io.*;
import java.text.*;
import java.util.*;

class alarm {

   public static void main(String[] args)
   {
      Calendar today = Calendar.getInstance();
      String outDate = "";
      String outTime = "";
      String period = "";
      int month = today.MONTH;
      int day = today.DAY_OF_MONTH;
      int year = today.YEAR;
      int dayPeriod = today.AM_PM;

      outDate = calcDate(month);

      switch(dayPeriod)
      {
      case 0: period = "AM";
         break;
      case 1: period = "PM";
         break;
      };

      outDate = outDate + " " + day;
      outDate = outDate + ", " + year;

      outTime = today.HOUR + ":" + today.MINUTE + ":";
      outTime = outTime + today.SECOND;
      outTime = outTime + " " + period;

      outDate = outDate;
      System.out.println("Today's Date: " + outDate);
      System.out.println("");

      System.out.println("Today's Time: " + outTime);
   }

   public static String calcDate(int month)
   {
      String  fullMonth = "";

      switch(month)
      {
      case 0: fullMonth = "January";
         break;
      case 1: fullMonth = "Feburary";
         break;
      case 2: fullMonth = "March";
         break;
      case 3: fullMonth = "April";
         break;
      case 4: fullMonth = "May";
         break;
      case 5: fullMonth = "June";
         break;
      case 6: fullMonth = "July";
         break;
      case 7: fullMonth = "August";
         break;
      case 8: fullMonth = "September";
         break;
      case 9: fullMonth = "October";
         break;
      case 10: fullMonth = "November";
         break;
      case 11: fullMonth = "December";
         break;
      }; //end switch statement block.

      return fullMonth;
   }

}


And here is the output I get so far.

Quote:
Today's Date: March 5, 1

Today's Time: 10:12:13


I am not looking for answers, but just ideas of what I am doing wrong or hint on where to go to find the solution. Thank you in advance for any and all help.


Top
  Profile  
 
 Post subject:
PostPosted: Wed Jul 20, 2005 9:55 pm 
8086
8086

Joined: Thu Jun 16, 2005 12:24 pm
Posts: 36
Location: canada
outDate = calcDate(month);
at a glance shouldnt it be MONTH not month


Top
  Profile  
 
 Post subject:
PostPosted: Thu Jul 21, 2005 11:13 am 
Thunderbird
Thunderbird
User avatar

Joined: Wed Jul 07, 2004 1:13 pm
Posts: 817
Location: Missouri
I have the data from the calendar MONTH being stored in a variable named month. This is what I am passing to this method.


Top
  Profile  
 
 Post subject:
PostPosted: Thu Jul 21, 2005 12:23 pm 
Million Club - 5 Plus*
Million Club - 5 Plus*
User avatar

Joined: Sun Sep 12, 2004 6:37 pm
Posts: 4745
Location: In the monkey's litterbox
You don't use Calendar.MONTH, which is static const int, you use today.get(Calendar.MONTH)
Also, try using DateFormat.
http://www.javaworld.com/javaworld/jw-1 ... dates.html

BTW, I have working code. ;)


Top
  Profile  
 
 Post subject:
PostPosted: Sat Jul 23, 2005 4:43 pm 
Thunderbird
Thunderbird
User avatar

Joined: Wed Jul 07, 2004 1:13 pm
Posts: 817
Location: Missouri
But I thought since I made today an instance of the Calendar object, I could just access the MONTH constant like I did everything else. I will try it your way. I have the month working fine and all. I do not have the year coming through. If you display the entire object throught the command prompt you can see that YEAR=2005, which I want to store in the integer of my choosing.


Top
  Profile  
 
 Post subject:
PostPosted: Sat Jul 23, 2005 5:50 pm 
Million Club - 5 Plus*
Million Club - 5 Plus*
User avatar

Joined: Sun Sep 12, 2004 6:37 pm
Posts: 4745
Location: In the monkey's litterbox
public static final int MONTH = 2
public static final int DAY_OF_MONTH = 5
public static final int YEAR = 1
source: Java API contants

Those are why you are getting March 5, 1. You have to do the today.get(Calendar.MONTH), etc, to get what you want.


Top
  Profile  
 
 Post subject:
PostPosted: Sat Jul 23, 2005 8:30 pm 
Thunderbird
Thunderbird
User avatar

Joined: Wed Jul 07, 2004 1:13 pm
Posts: 817
Location: Missouri
You are correct. Once I used .get(Calendar.MONTH) for whatever value I was trying to obtain I got the correct values to be stored in variables later.

I now having it working great and now on to the next phase. To implement this in JFrame, which is a simple process that I did a lot in the adv java programming course I just finished. I really like the absolute position available in java. It give a huge amont of control in the appearance of the GUI.


Top
  Profile  
 
 Post subject:
PostPosted: Wed Jul 27, 2005 7:19 pm 
Bitchin' Fast 3D Z8000*
Bitchin' Fast 3D Z8000*
User avatar

Joined: Tue Jun 29, 2004 11:32 pm
Posts: 2555
Location: Somewhere between compilation and linking
baldeagle wrote:
But I thought since I made today an instance of the Calendar object, I could just access the MONTH constant like I did everything else. I will try it your way. I have the month working fine and all. I do not have the year coming through. If you display the entire object throught the command prompt you can see that YEAR=2005, which I want to store in the integer of my choosing.


The instance of the Calendar object that you've instantiated stores the date information in non-public fields that can only be access via the get and set methods of the class. As you're probably already aware, this is called encapsulation - let's see who gives the most cogent answer about why it is done this way. ;)

The constant fields like MONTH, DAY and JANUARY are an enumeration so that your code is readable - cal.get(YEAR) or cal.set(JULY) instead of cal.get(4) or cal.get(1). The API states this pretty clearly:

static int MONTH
Field number for get and set indicating the month.

There is some good discussion in the API on how to use Calendar. Also, you should be using a DateFormat object to create the actual output.

http://java.sun.com/j2se/1.5.0/docs/api/

Date is in java.util
DateFormat is in java.text


Top
  Profile  
 
 Post subject:
PostPosted: Wed Jul 27, 2005 8:38 pm 
Thunderbird
Thunderbird
User avatar

Joined: Wed Jul 07, 2004 1:13 pm
Posts: 817
Location: Missouri
Encapsulation is done to limit users access to variables and methods within a class and protecting these class defined objects. This method of access is done to protect the data from accidently being changed or modified and/or being directly access by any user of a program utilizing any class using encapsulation.


Top
  Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 9 posts ] 

All times are UTC - 8 hours


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group