Author Topic: Sharing this Java Code on Day Finder  (Read 134 times)

0 Members and 1 Guest are viewing this topic.

Offline JusticeLeague

  • Sr. Member
  • *
  • Posts: 356
  • Karma: +1/-0
    • View Profile
Sharing this Java Code on Day Finder
« on: August 28, 2008, 12:48:34 PM »
Machine Problem:
A program that asks for a number, and uses that number to find the date that many days from today (or whatever day you\'re using it on). The code was adopted from a homework assignment, so that\'s why the variable names might not make a lot of sense, I was too lazy to change them. The program still works fine though.

Solution:

Code: [Select]
import java.util.GregorianCalendar;
import java.lang.String;
import java.util.Scanner;

public class dateFinder
{
    public static void main(String[] args)
    {   GregorianCalendar cal = new GregorianCalendar();   //declare today\'s date
 
        System.out.println("Please enter a number greater than 0:");
        Scanner keyboard = new Scanner(System.in);
        String numberA = keyboard.next();

        int valueA = Integer.parseInt( numberA );          
        //Thanks to sciwizeh for the code
       
        cal.add(GregorianCalendar.DAY_OF_MONTH, (valueA));
       
        //int variables for normal (cal) Gregorian Calendar    
        int month = cal.get(GregorianCalendar.MONTH);          
        int year = cal.get(GregorianCalendar.YEAR);
        int dayOfMonth = cal.get(GregorianCalendar.DAY_OF_MONTH);
        int weekday = cal.get(GregorianCalendar.DAY_OF_WEEK);
       
        //============= End Regular Calendar ints
       
        //Changes numbers to days of week (i.e. 0 = Sunday, 6 = Saturday)

        String days100weekday = new String();
        days100weekday = ("" +weekday);
        days100weekday = days100weekday.replace("1", "Sunday");        
        days100weekday = days100weekday.replace("2", "Monday");
        days100weekday = days100weekday.replace("3", "Tuesday");
        days100weekday = days100weekday.replace("4", "Wednesday");
        days100weekday = days100weekday.replace("5", "Thursday");
        days100weekday = days100weekday.replace("6", "Friday");
        days100weekday = days100weekday.replace("7", "Saturday");
       
        //============= End days100weekday portion
             
        //Change numbers to month name (i.e., 0 = Jan., 12 = Dec.)
 
        String days100month = new String();
        days100month = (", " +month);
        days100month = days100month.replace("11", "December");          
        days100month = days100month.replace("10", "November");
        days100month = days100month.replace("9", "October");
        days100month = days100month.replace("8", "September");
        days100month = days100month.replace("7", "August");
        days100month = days100month.replace("6", "July");
        days100month = days100month.replace("5", "June");
        days100month = days100month.replace("4", "May");
        days100month = days100month.replace("3", "April");
        days100month = days100month.replace("2", "March");
        days100month = days100month.replace("1", "February");
        days100month = days100month.replace("0", "January");      

        //============= End days100month portion        
       
        String days100dayOfMonth = new String();
        days100dayOfMonth = (" " +dayOfMonth);
       
        //============= End days100dayOfMonth portion
       
        String days100year = new String();
        days100year = (", " +year);
       
        //============= End days100year portion
       
        String period = new String();
        period = (".");
       
        //============= End period portion
       
        System.out.println("In x number of days, it will be: "+days100weekday +days100month +days100dayOfMonth +days100year +period);
    }  
}

Techronnati | where technology never sleeps

Sharing this Java Code on Day Finder
« on: August 28, 2008, 12:48:34 PM »

Mountain View

 

Related Topics

  Subject / Started by Replies Last post
0 Replies
116 Views
Last post October 13, 2008, 02:58:40 PM
by h2obubbler
0 Replies
158 Views
Last post October 13, 2008, 03:07:51 PM
by h2obubbler
0 Replies
87 Views
Last post October 19, 2008, 04:09:28 PM
by MrSpecialist
0 Replies
70 Views
Last post December 10, 2014, 06:55:24 AM
by MoneyRepublic
1 Replies
119 Views
Last post November 29, 2016, 02:52:03 AM
by h2obubbler

Posting Disclaimer: Any individual may post a message in this forum and may do so anonymously. Therefore, the sole author is exclusively and entirely responsible for all opinions in that message. They do not represent the official opinions of Techronnati, its administrators or moderators or the Techronnati Management. Techronnati is merely acting as an impartial conduit for constitutionally protected free speech and is not responsible and will not be held liable for the content of such messages. All images and service logos are trademarks of their respective owners.