Link to home
Start Free TrialLog in
Avatar of joejavainbahm
joejavainbahm

asked on

date converter

basic java program that reads from uses and converts dates from numerical month-day format
to alphabetic month- day format  example  1/31 or o1/31 to january 31
with 2 exception classes one for month one for day  make february always 29 days to make it easy!
????
Avatar of Mick Barry
Mick Barry
Flag of Australia image

You can use a SimpleDateFormat.
Use one format to parse the date, and another to output in different format.
Avatar of somasekhar
somasekhar

hi
see this link

http://www.koders.com/java/fid6E48BF85D9FF65A8D970CF20552008CD737D37D8.aspx

execute method will shows different ways how you can format date.
find more documentation about SimpleDateFormat here

http://java.sun.com/j2se/1.4.2/docs/api/java/text/SimpleDateFormat.html
Homework?
Jmasimon ,

Doing somebody's home-work is against EE rules. Pls let joejavainbahm comes up with some efforts of his own.
Only if he tells me that he is a student and doing his homework, then i wouldn't do it.
But since he hasn't I can't assume such a thing.
Just now I made wrong assumption. Hence 'he'. i don't know that really!

Well, if you read that question, he is just ordering us to write some program and 'demanding' for code ;-) which is treated like home-work most of the times. It looks like a copy-paste of an assignment which a teacher would've given to him :) Anyway, we can't even post full-code if somebody asks for it just like that.
Just because you suspect that he might be cheating, doen't mean he's cheating.
We don't know that. Also I didn't write full comment in my code and even if this is his assignment he'll have to understand my code. If he does he's a champ...

If he doesn't he'll have to look further into it so either way he will have to put some effort into this.
Anyhow it's not my business and I don't care what he does with my code. All I did is just for fun.

If he wants to cheat then that is his will, he's just cheating on himself.
When we sign up we all have to agree to EE rules and therefore we are mature enough to
aware of it.

My code is not a solution and should not consider as solution. Rather it's a guide on how to
manipulate data in different logical ways.

Solution is only when he make use of it and does what it meant to for his overall requirement.
This question might be some part of his work therefore can't assume that it's complete solution
for overall his work. I'm just trying to point to right direction.

It might not even be right unless if he confirms it. I tried to write it close to his requirements.

This code only reflects on my logical thinking in order to express my idea. Hence that is why I
posted as comment.
I'm not trying to argue with you on this, Jmasimon. All I'm trying to say is that moderators have warned experts who did exactly what you did in this question, and no matter whether it is home-work or not, we still cannot provide full-code for any question just like that. And the amount of code that you have given is pretty substantial.
Avatar of joejavainbahm

ASKER

hi people just trying to learn java on my on no teacher or book sorry for any problems ill
just cancel the account after the 30 days i paid for unless that guy is willing to REFUND!!!!!
did not mean to cause any harm !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!



ps thanks for anyone that tryed to help

What's your current status? Have you been able to write some code on your own?
ASKER CERTIFIED SOLUTION
Avatar of Jmasimon
Jmasimon

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
sorry. there is typo there.
Meant to be "public class MyException extends Exception"
Once you have your own exception class you can throw exception. In order to do that you have to flag which exception is going be, so this is how you can flag.

Assume you have this method which you want to throw exception.
public String parseDateToDateString(String date)

then you will change it to
   public String parseDateToDateString(String date) throws MyException, MyExcepton2

you can define more that one exception to be thrown at (just like above)...

Once you have done that, anywhere in that method you can throw those exceptions defined above...
This is how you throw exception by using 'throw' keyword.

    throw new MyException();

The statement above will create new object called MyException and use that to throw exception.
When new object MyException is created, it will invoke the default constructor 'public MyException()'. However you are not limited to just this constructor, you can define your own.
Ask me if you want to know more on this.

Once again, you can throw exception anywhere in the method above. So for example it can be in
logical loop statement, so it will only throw when the logical statement is true.

For example.        
       if( !isValidDate(iDate) )
       {
               throw new MyException();
       }

       if( !isValidMonth(iMonth) )
       {
               throw new MyMonthException();
       }
If you want to learn java, you will at least need to know the following java keywords.
    if, while, switch, public, protected, private, class, interface, implements, extends, synchronized (may be not, but worth to note),final, do, return, break (avoid using them in loops, try to exist normal), throw, throws, new, static.

pri variable type...
    String, int, boolean, long, double, float (avoid using them, may result in inaccurate result even makes you wonder).

Statement to printout on screen...

   This will output to standard out, which appear on screen.
     System.out.print("hi");
     System.out.println("Hello");

    This will output to standard error
        System.err.print("123 - Invalid date format");
        System.err.println("123 - Invalid month, there are ONLY 12 months.");

'println' prints out new line, but 'print' doesn't.
I left out very important java keywords for error handling. they are 'try, catch'.

the usage is like this...

    try
    {
         // Write you code
    }
    catch( // you will need to specify which exception you are trying to catch )
    {

    }

Recall example above, where you specify the method that will throw exception...

    public String parseDateToDateString(String numDate) throws MyException, MyException2

So I you are going called that function, it will throw MyException or MyException.
When the exception is thrown and you don't use 'try' to try it out first and then 'catch' to catch if any exception is thrown you lose control of your program flow. The JVM (Java virtual Machine will take care of that).

But that is not how you should program. Always need to guide the program flow so that it does what you wanted rather stops somewhere where it hits.

     So for the function above, you will need to try it out first because there is flag that telling you that it will at some stage it will throw exception. If you don't called that function in try scope, you will get compilation error stating that you havn't caught the exception for it. So this is how you can...

       try
       {
              String dateString = null;

              dateString = parseDateToDateString("1/30");

       }
       catch( MyException me )
       {
             System.out.println( me.getMessage() );
       }
       catch( MyException2 me2 )
       {
             System.out.println( me2.getMessgae() );
       }

As you can see, you can catch serval exception, not just one. However if it matches to one of the catch it will not follow on to other catches to see if there's a match.

Notice how I'm calling 'me.getMessage()' from MyException class. You don't have that method but you extended your class from 'Exception' class so you inherited so the 'getMessage()' function is actually from 'Exception' class which 'MyException' class extended from.

thanks to all that helped , for now i think i got it ? maybe? ill let you know soon

thanks
In java you can have array of elements or a list of the same type or objects.

So you can have array of String, int, and so on.

So in your case if you will need to have a way of get the month string depending on the month number given by the user. So you will need to have String variable, hence "month string".

If you just have String variable, it's not enough to lookup all the months. But we can look up one of the 12 months. How about if you have 12 String variable the hold all the month in word.

So then kind of have to look up the month number specified by user and return back the corrent variable. So something like this...

        In pseudocode
               Module: getMonthString

               Import: iMonth As Integer
               Export: strMonth As String

               Local Variable: month1 As String, initialize to "January",
                                      month2 As String, initialize to "February", and so on until
                                      month12 As String, initialize to "December"

               pseudocode:

                    IF iMonth EQUAL TO 1
                         RETURN month1
                   
                    IF iMonth EQUAL TO 2
                          RETURN month2

                    .... and so on until

                    IF iMonth EQUAL TO 12
                          RETURN month12

By doing this way, you code will get very long and have to visit to every statement in total of 12 times. Or you can put it in SWITCH statement but still have to write up 12 returns.

How about if you make use of array, them you  can put all all the month string in order.
So this is how you declare array type.

     First. variable access type - private, public or protected
     Second. variable type - String, int, and so on. You can have array of object as well.
     
     For example to DECLARE private String array...

           private String monthString[];

     You can also DEFINE at the same line where you are declaring by using '{' and closing with '}'

           private String monthString[] = {"January", "Feb...", and so on then close with '}';

When you put then into array, it like putting them into table with 1 column and you can retrieve them if you know which row they are in. In java the row index start from 0 and N -1. N is number of items you put in and not equal to 0 (N > 0). So now you know the start index and last index (N-1).

This is how you can access them...
         monthString[0];

or
         int monthNum = 9;

         String monthStr = monthString[monthNum - 1];

         Because row index starts at 0 and not at 1, you will need to minus 1 to get look up in correct
         position.

>>My code is not a solution and should not consider as solution. Rather it's a guide on how to manipulate data in different logical ways.

>> Your code is full code solution to the asked question. And if the Asker give it to their teacher, they'ss probably get an A (or B in the worst case).

Assuming that the teacher is stupid that don't even know they are cheating.

>> If you want to show an idea, use pseudo-code, cite the names of the functions taht should be usewd, bu do not show how to make it.

Right! I think you have motivated me, so I should get on with it.
Rather then bottom up approach, I should have done top-down approach.

>> Assuming that the teacher is stupid that don't even know they are cheating.

In many assignments, students don't bother about that. They just submit the files on time and bother about studying later (in the last minute before the exam ;-)) Unfortunately, many such students ask questions at EE. I'm not saying that this is a similar case.
Assuming that they will get marks for it! Big Mistake, might even fail the exam or worse gets kick out.
Like I said, they only care about the current assignment, not the future ;)
Good old Venabili back in form :) nice to see that. Keep up the good work.