Hi guys and girls.
I’m a bit of a newbie and I hope I haven’t posted in the wrong thread but I need a little help. I am trying to mess about with Greg calendar. I have been told to make a diary app and was hoping to use this as not to clog everything up with strings, that and we need to implement the deletion of past appointments and stop overlapping so I need calendar. Only problem is we haven’t learnt it yet and I'm ripping out what little help I have. If I post some code can someone at least tell me why it’s on about a return statement? I know this code runs with strings but not with this int business. The other thing is where I declare what the year month day date and so forth. If I swap these variables round can I get the date to display differently? Cheers peeps the code is below.


import java.text.DateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.TimeZone;
public class Time
{
private int year;
private int month;
private int day;
private int hour;
private int minute;


//Constructor creates a Time initialised to parameters


public GregorianCalendar Calender(int y, int m, int d, int h, int mi)
{
year=y;
month=m;
day=d;
hour=h;
minute=mi;
}

public int getMonth() {
return month;
}
}

Recommended Answers

All 18 Replies

The overall aim of this is to use a readkeybored method to take each variable and make a date for an appointment but I think thats just gonna blow me away. I have done all this and the application with strings but i really want to be a show pony and try somthing that makes sense.

Hi Ezzaral.
I have looked through it but it just seems to me that its all setting timezone and locale. Like i said I promis your not doing my homework as iv finished my assignment with strings but i just want it to be functional aswell as just finished.
Ignor the getmonth method. Still it wont comply. This is how i declared the variables for my strings but it wont work for the calendar.

Just to show you I have done somthing today :D this is the same class but with my strings

import java.util.*;
import java.io.*;
/**
* The Class Appointment deals with the entry of data for new appointments.
*
* @Joseph Cummins
* @Last Revised 04-12-2007
*/
public class Appointment
{
private String name;
private String phone;
private String info;
private String day;
private String date;
private String time;
private String length;


/**
* Constructors
*/


public Appointment()
{
name="unknown";
}


public Appointment(String n, String p, String fo, String day, String da, String t, String l) {
name=n;
phone=p;
info=fo;
day=day;
date=da;
time=t;
length=l;
}
/**
* Methods
*/


public void readKeyboard()
// Used in DiaryApp to add details to a new appointment.
{
Scanner in=new Scanner(System.in);
System.out.print("**Enter name of the person you are meeting with: ");
name=in.nextLine();
System.out.print("**Enter the contact telephone number: ");
phone=in.nextLine();
System.out.print("**What is the nature of the meeting. Use one word eg(Dentist): ");
info=in.nextLine();
System.out.print("**What day is the meeting: ");
day=in.nextLine();
System.out.print("**Please enter the date of the meeting eg(21-03-2007): ");
date=in.nextLine();
System.out.print("P**lease enter the time of your meeting in the 24hour clock format eg(0800): ");
time=in.nextLine();
System.out.print("**Please enter the length in minutes of the meeting eg(2hr 30mins = 150): ");
length=in.nextLine();
}



public String toString()     {
//This tostring is used in the Printout method to list the contence of the diary file.
return " **Contact name: "+name+"\n **Contact Phone: "+phone+"\n Type of meeting: "+info+"\n **Day of meeting: "+day+
"\n **Date of meeting: "+date+"\n **Time of meeting: "+time+"\n **Length of meeting: "+length;
}
public String Output() {
// This string is used in the load and save methods so the file is saved correctly
return name+"  "+phone+" "+info+" "+day+" "+date+" "+time+" "+length;
}


}

you see by matering this bloody calendar i could make this redundent. If you think this is beyond my reasoning then please say as i shall give up and stick with my strings. Im just a show off realy.

You don't need to have all of the separate variables in your program to use the GregorianCalendar. It encapsulates all of that in itself. You use it by setting the fields as needed, adding or subtracting various time increments, comparing different Calendar (date) instances, etc. Perhaps these examples will help a bit more:
http://www.exampledepot.com/egs/java.util/GetDateFromCalendar.html?l=rel

Working with the calendar can be a bit non-intuitive at first, as you probably see by now. Take the time to read through the API document for it, the methods available on it, and the tutorials you can find.

(Edit: I was referring to the original code you posted when I wrote this. Not the code above that you added in the mean time :) )

Mr Ezzaral you a star person for helping me at half past 10 in the evening. you say messing woth time incaments. What if i just wanted the times blank and the user fill them in when they run the readkeybord. Ie asking them for each variable to be stored infile. Is that possible? as you can see from my string class I just want it to make strings redundent so i can call one "date" instead of hundreds of strings.

You would use the inputs to construct a GregorianCalendar instance that represents the date and time they entered. Keep in mind, you would still have to write that info into your file and if comparing to another appointment, read the info from file to construct the Calendar instance that represents the other appointment.

what would you think about splitting the date and time seperate making use of date and time methods?

That is not neccessary, date and time get calculated from same offeset since epoch in miliseconds

Hey all. I regret to inform that me and greg the calendar application fell out last night and are now not speaking untill my lecturer rectifies our hatred for eachother. I am going to try and write it with date and time just to see if i can do that thing with removing old appointments. and stuff. Thanks for your help dude but i think i ned more teaching :D

hey guys. New issue, been working like a crazy man recently. I have statrted woth my greg code and have got this far. Im getting a constructor error and i think iv lost my head. the variables have been converted from string to int and the application was working but returning null on the printout method. cousdl you take a look at my code and rip it to shreds with constructive critasism. Be gentle this thing is killing me :)

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

/**
 * The Class Appointment deals with the entry of data for new appointments.
 * 
 * @Joseph Cummins   
 * @Last Revised 04-12-2007
 */
public class Appointment
    implements Comparable<Appointment> //needed for sort
{
private GregorianCalendar calendar;    
private String name;
private String phone;
private String info;
private Integer length;
//private String longDate;
//private String longTime;
//private Integer date;
//private Integer month;
//private Integer year;
//private Integer hour;
//private Integer minute;



  public Appointment()
    {
        name="unknown";

    }

  /*  public Appointment(String n, String p, String fo, int da, int m, int y, int hr, int min, int l) {
        name=n;
        phone=p;
        info=fo;
        calendar = new GregorianCalendar ( da, m, y, hr, min);
        length=l;
}*/
    static final SimpleDateFormat sdf =
            new SimpleDateFormat( "EEEE dd/MM/yyyyy HH:mm:ss " );



/**
 * Methods
 */






    public void readKeyboard() throws IOException
    // Used in DiaryApp to add details to a new appointment. 
    {
        Scanner in=new Scanner(System.in);
        System.out.print("**Enter name of the person you are meeting with: ");
        name=in.next();
        System.out.print("**Enter the contact telephone number: ");
        phone=in.next();
        System.out.print("**What is the nature of the meeting. Use one word eg(Dentist): ");
        info=in.next();
        System.out.print("**What is the date of the meeting (DD/MM/YYYY): ");

        String longDate=in.next();
        String da=longDate.substring(0,2);
        String m=longDate.substring(4,5);
        String y=longDate.substring(7,10);

        // Conversion of string back to int.
        int daint =Integer.parseInt(da);
        int mint =Integer.parseInt(m);
        int yint =Integer.parseInt(y);
        System.out.print("**Enter the the time in the 24Hour format with !!no grammer!! (eg 2345): ");
        String longTime=in.next();
        String hr=longTime.substring(1,2);
        String min=longTime.substring(3,4);
        // converting The stings back to ints.
        int hrint =Integer.parseInt(hr);
        int minint =Integer.parseInt(min);
        System.out.print("**Please enter the length in minutes of the meeting eg(2hr 30mins = 150): ");
        length=in.nextInt();
        calendar = new GregorianCalendar ( da, m, y, hr, min);
   }


   public String toString()     {
       //This tostring is used in the Printout method to list the contence of the diary file.
        return " **Contact name: "+name+"\n **Contact Phone: "+phone+"\n Type of meeting: "+info+"\n **Date of meeting: "+calendar+"\n **Length of meeting: "+length;
    }
    public String Output() {
    // This string is used in the load and save methods so the file is saved correctly
        return name+"  "+phone+" "+info+" "+calendar+" "+length;
    }
   public int compareTo(Appointment other) {
        return (this.calendar).compareTo(other.calendar);
    }
}

Where is the main method that you are executing? You only have one valid constructor (the other is commented out) and very little is initialized in that default constructor.

Sorry dude i fixed that. The constructor at the bottom of the readkeybored was looking for sting vars not ints. fixed like a dude now. Secondly. My application calls a printout method that you can allso see at the bottom, when the print happens I get the printout
"java.util.GregorianCalendar[time=?,areFieldsSet=false,areAllFieldsSet=false,lenient=true,zone=sun.util.calendar.ZoneInfo[id="Europe/London",offset=0,dstSavings=3600000,useDaylight=true,transitions=242,lastRule=java.util.SimpleTimeZone[id=Europe/London,offset=0,dstSavings=3600000,useDaylight=true,startYear=0,startMode=2,startMonth=2,startDay=-1,startDayOfWeek=1,startTime=3600000,startTimeMode=2,endMode=2,endMonth=9,endDay=-1,endDayOfWeek=1,endTime=3600000,endTimeMode=2]],firstDayOfWeek=2,minimalDaysInFirstWeek=4,ERA=?,YEAR=29,MONTH=3,WEEK_OF_YEAR=?,WEEK_OF_MONTH=?,DAY_OF_MONTH=985,DAY_OF_YEAR=?,DAY_OF_WEEK=?,DAY_OF_WEEK_IN_MONTH=?,AM_PM=0,HOUR=9,HOUR_OF_DAY=9,MINUTE=0,SECOND=0,MILLISECOND=?,ZONE_OFFSET=?,DST_OFFSET=?]"

Is there anyway to print the contence of the calander after it was made. nearly there guys

You need to use a formatter to convert the Calendar instance to a desired date/time display format. Here is the simplest example

java.util.Calendar cal = java.util.Calendar.getInstance();
String date = java.text.SimpleDateFormat.getInstance().format(cal.getTime());

That uses the default "short" date-time format of SimpleDateFormat. There are many other variations that you can specify if you wish. Take a look at the SimpleDateFormat API and DateFormat:
http://java.sun.com/javase/6/docs/api/java/text/SimpleDateFormat.html
and this tutorial information
http://java.sun.com/docs/books/tutorial/i18n/format/dateintro.html

Hey dude please dont shout but i gave up with greg. Stuck to the basics but oneday i will return to you Ezzaral with the best damn diary application you have ever seen :). Seriously though cheers for the help it was a good experiance and i acctually learned alot in the process of digging around in the api and countless websites. Keep you eyes open as iv just posted again.

Member Avatar for adpeate

Hi chaps,

im sort of continuing where Jezeral stopped - were in the same class here at uni.

I was wondering how the data generated by gregoriancalender is used.

Assuming i have a reasonably similar solution to Jez - well same basic idea - create an instance of gregorian to store time and date data for appointments, and iv got the thing displaying the time in readable format (im writing this at the moment!), how would i go about writing this time and date related data into a file and loading it back into the system?

I can do basic save/load methods fine but im not sure how to handle gregoriancalender data in these cases.

Apologies if this post is considered hijacking by the way

many thanks in advance,
Al

Hi chaps,

im sort of continuing where Jezeral stopped - were in the same class here at uni.

Assuming i have a reasonably similar solution to Jez and iv got the thing displaying the time in readable format (im writing this at the moment!), how would i go about writing this time and date related data into a file and loading it back into the system?

I can do basic save/load methods fine but im not sure how to handle gregoriancalender data in these cases.


many thanks in advance,
Al

SimpleDateFormat has methods to format a date to a string and to parse a string to a date. If you are using a particular format, you can format the date before you write it into the file and parse it when you read it back out.

Member Avatar for adpeate

Right i see. i will attempt to put it into action and let you (and Jez) know the outcome

Many thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.