Member Avatar for TheFearful

I am new to Java and am experimenting with the language. I am looking a way to convert a user-inputted date and convert it to the MSexcel format. So 1998/07/05 is 35981. Any clue on how to do this?
This is what I have so far

import java.util.*;
import java.text.*;

public class A1Q1
{
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
        int year;
        int sv1;
        int month;
        int sv2;
        int day;

        System.out.println("Enter a time in the format yyyy/mm/dd");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
        try
        {
        String userDate = input.next();
        Date date = sdf.parse(userDate);

        System.out.println(date);
        }
        catch(ParseException e)
        {
            e.printStackTrace();
        }
    }
}
Member Avatar for TheFearful

I changed it to this for now, but how do I get it so that I can input it as one whole date?

ex) 1990/12/25 is my goal, but I can only type it as 1990 / 12 / 25
How do I fix this without making it all a string?

import java.util.*;
import java.text.*;

public class A1Q1
{
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
        int year;
        char sv1;
        int month;
        char sv2;
        int day;

        int hour;
        int minute;
        int second;
        int format;
        int end = 0;

        System.out.println("Enter a time in the format yyyy/mm/dd");


        year = input.nextInt();
        sv1 = input.next().charAt(0);
        month = input.nextInt();
        sv2 = input.next().charAt(0);
        day = input.nextInt();
        System.out.println(year + "" + sv1 + "" + month + "" + sv2 + "" + day);

        System.out.println("Enter a time in the format hh:mm:ss");
        hour = input.nextInt();
        sv1 = input.next().charAt(0);
        minute = input.nextInt();
        sv2 = input.next().charAt(0);
        second = input.nextInt();

        System.out.println("Which excel date do you want it converted to? 1) Windows 2)Mac");
        do
        {
            format = input.nextInt();
            if(format == 1 || format == 2)
            {
                end = 1;
            }
            else
            {
                System.out.println("Please pick 1 for Windows or 2 for Mac");
            }

        }
        while(end == 0);
        if(format == 1)
        {
            System.out.println("Windows");
        }

        else if(format == 2)
        {
            System.out.println("Mac");
        }
    }
}

Use / as a delimiter for your Scanner

Member Avatar for TheFearful

I tried using it as a delimiter, but I am getting an error with it. Anything wrong with what I am doing?

Scanner input = new Scanner(System.in);
        input.useDelimiter("[/\n]");
        int year;
        char sv1;
        int month;
        char sv2;
        int day;
        double eM;
        double newMonth;
        double aYear;
        double leap;
        double conversion = 0;
        double dOfMonth;
        double result;

        int hour;
        int minute;
        int second;
        int format;
        double totalSeconds;
        double secondRatio;
        int end = 0;


        System.out.println("Enter a time in the format yyyy/mm/dd");


        year = input.nextInt();
        //sv1 = input.next().charAt(0);
        month = input.nextInt();
        //sv2 = input.next().charAt(0);
        day = input.nextInt();

        input.close();

"getting an error with it"? That's not helpful. Exactly what error where?

Member Avatar for TheFearful

This is the error I am getting and it is pointing to

day = input.nextInt();





Exception in thread "main" java.util.InputMismatchException
    at java.util.Scanner.throwFor(Scanner.java:909)
    at java.util.Scanner.next(Scanner.java:1530)
    at java.util.Scanner.nextInt(Scanner.java:2160)
    at java.util.Scanner.nextInt(Scanner.java:2119)
    at A1Q1.main(A1Q1.java:65)

and what is the input you entered?

Member Avatar for TheFearful

1999/12/25

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.