CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   Java and JSP (http://www.codingforums.com/forumdisplay.php?f=54)
-   -   Please Help Resolve a Code Bug (http://www.codingforums.com/showthread.php?t=257307)

P@Boberg 04-14-2012 07:58 PM

Please Help Resolve a Code Bug
 
Any good samaritans out there with a spare 5 minutes?

Complete noob here, and I'm working on an assignment for a beginner Java course. Last week we made a simple Hello Server web program. This week we're editing last week's code to make a add() and display Calendar app.

Below is the entire code. It compiles and runs but instead of the current date minus/plus the input number the program returns...

Quote:

New Calendar Date, java.util.GregorianCalendar[time=1334343283825,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneIn fo[id="America/Chicago",offset=-21600000,dstSavings=3600000,useDaylight=true,transitions=235,lastRule=java.util.SimpleTimeZone[id=America/Chicago,offset=-21600000,dstSavings=3600000,useDaylight=true,startYear=0,startMode=3,startMonth=2,startDay=8,startDa yOfWeek=1,startTime=7200000,startTimeMode=0,endMode=3,endMonth=10,endDay=1,endDayOfWeek=1,endTime=72 00000,endTimeMode=0]],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2012,MONTH=3,WEEK_OF_YEAR=15,WEEK_OF_MONTH=2,D AY_OF_MONTH=13,DAY_OF_YEAR=104,DAY_OF_WEEK=6,DAY_OF_WEEK_IN_MONTH=2,AM_PM=1,HOUR=1,HOUR_OF_DAY=13,MI NUTE=54,SECOND=43,MILLISECOND=825,ZONE_OFFSET=-21600000,DST_OFFSET=3600000]!
So with further adieu the code:

Code:


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

/**
 * An example of subclassing NanoHTTPD to make a custom HTTP server.
 */
public class HelloServer extends NanoHTTPD
{
        public HelloServer() throws IOException
        {
                super(8080, new File(".").getAbsoluteFile());
        }

        public Response serve( String uri, String method, Properties header, Properties parms, Properties files )
        {
                System.out.println( method + " '" + uri + "' " );
                String msg = "<html><body><h1>Calendar Offset</h1>\n";
                if ( parms.getProperty("offset") == null )
                {
                        msg +=
                                /** This message is only to get the variable
                                The variable is sent via the <input>*/
                               
                                "<form action='?' method='get'>\n" +
                                "  <p>Enter Offset Amount: <input type='int' name='offset'></p>\n" +
                                "</form>\n";
                }
                else
                {
                        /** Create Calendar variable */
                       
                        Calendar now = Calendar.getInstance();
                       
                        /** Create and cast offset variable */
                       
                        int offset = Integer.parseInt(parms.getProperty("offset"));
                       
                        /** Add the offset to the current date */
                       
                        now.add(Calendar.DATE, offset);
                       
                        /** Display updated date */
                       
                        msg += "<p>New Calendar Date, " + now.toString() + "!</p>";
                        // "</form>\n" + add(Calendar.DATE,offset);
                }

                msg += "</body></html>\n";
                return new NanoHTTPD.Response( HTTP_OK, MIME_HTML, msg );
        }


        public static void main( String[] args )
        {
                try
                {
                        new HelloServer();
                }
                catch( IOException ioe )
                {
                        System.err.println( "Couldn't start server:\n" + ioe );
                        System.exit( -1 );
                }
                System.out.println( "Listening on port 8080. Hit Enter to stop.\n" );
                try { System.in.read(); } catch( Throwable t ) {};
        }
}

Again, I'd really appreciate your help. I'm a little lost in the weeds here. PLEASE HELP!

sean3838 04-15-2012 03:24 PM

You may need to format your date first. Import java.text.DateFormat; then try:

Code:

                        /** Create Calendar variable */
                       
                        Calendar now = Calendar.getInstance();
                       
                        /** Create and cast offset variable */
                       
                        int offset = Integer.parseInt(parms.getProperty("offset"));
                       
                        /** Add the offset to the current date */
                       
                        now.add(Calendar.DATE, offset);
                        DateFormat newDate = DateFormat.getDateInstance(DateFormat.LONG);
                        String newDateString = newDate.format(now);


                        /** Display updated date */
                       
                        msg += "<p>New Calendar Date, " + newDateString + "!</p>";
                        // "</form>\n" + add(Calendar.DATE,offset);



All times are GMT +1. The time now is 05:35 PM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.