Day finder

llemes4011 0 Tallied Votes 94 Views Share

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.

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);
    }   
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.