Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 04-14-2012, 07:58 PM   PM User | #1
P@Boberg
New to the CF scene

 
Join Date: Apr 2012
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
P@Boberg is an unknown quantity at this point
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!
P@Boberg is offline   Reply With Quote
Old 04-15-2012, 03:24 PM   PM User | #2
sean3838
New Coder

 
Join Date: Jan 2012
Posts: 90
Thanks: 1
Thanked 13 Times in 13 Posts
sean3838 is an unknown quantity at this point
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 03:32 PM..
sean3838 is offline   Reply With Quote
Reply

Bookmarks

Tags
code error, noob

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 03:39 AM.


Advertisement
Log in to turn off these ads.