Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Apr 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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...

    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!

  • #2
    New Coder
    Join Date
    Jan 2012
    Posts
    97
    Thanks
    1
    Thanked 13 Times in 13 Posts
    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);
    Last edited by sean3838; 04-15-2012 at 04:32 PM.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •