0

I'm working on a date class project. The user has 3 options to input the data.

a) MM/DD/YYYY

b) Month DD,YYYY (June 27, 2000)

c) DDD/YYYY (180 2014) - Days of the year

The ouput will be in all 3 formats. I'm beginner. I got stuck and have no ideas how to start the program. I think we may need a constructor for each format.

a) int mm, int dd, int yyyy
b) string month, in dd, in yyyy
c) int ddd, int yyyy

And we can use switch for the input. Also, we need to check for the leapYear. The hard part is I do not know how to convert from this form to others. Here what I got so far:

public class Implementation 

    {
        private int day;
        private int month;
        private int year;
        private final String monthNames[] = {"Jannuary", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
        private final int monthDays[] = {31,28,31,30,31,30,31,31,30,31,30,31};

Can someone please help me? I'm very frustrated for this.

Jin Nguyen
  • 45
  • 10
  • 1
    Assuming that you are allowed to use Java 8: the oracle tutorials are always a good source to get into a specific topic; and there is a whole chapter on parsing/formatting: https://docs.oracle.com/javase/tutorial/datetime/iso/format.html – GhostCat Mar 27 '15 at 13:52
  • 1
    Please just Google some tutorials/examples on how to parse dates as a string using date formats. – tnw Mar 27 '15 at 13:53

3 Answers3

0

Use Simple Date Format. The docs tell you how to build the format string you pass to it.

You can basically do new SimpleDateFormat("your-format-here").format(date);, and it'll spit out the formatted date string for you.

Simple Date Format also works for parsing dates; just call .parse(stringRepresentation), and get a Date instance back.

David
  • 2,602
  • 1
  • 18
  • 32
0

Find more on Date parsing here

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

class DateConversion {
    public static void main (String[] args) throws java.lang.Exception {
        SimpleDateFormat simpleDateFormat_DDMMyyyy = new SimpleDateFormat("DD/MM/yyyy");
        SimpleDateFormat simpleDateFormat_MMMddyyy = new SimpleDateFormat("MMM dd, yyyy");
        SimpleDateFormat simpleDateFormat_DDDyyyy = new SimpleDateFormat("DDD yyyy");       

       // Converting String into java.Date
        Date date_DDMMyyyy = simpleDateFormat_DDMMyyyy.parse("27/06/2000");
        Date date_MMMddyyy = simpleDateFormat_MMMddyyy.parse("June 27, 2000");
        Date date_DDDyyyy = simpleDateFormat_DDDyyyy.parse("180 2000");
        System.out.println(date_DDMMyyyy);
        System.out.println(date_MMMddyyy);
        System.out.println(date_DDDyyyy);

        //Converting java.Date into String
        Date inputDate = new GregorianCalendar(2000, Calendar.JUNE, 27).getTime();
        String dateStr_DDMMyyyy = simpleDateFormat_DDMMyyyy.format(inputDate);
        String dateStr_MMMddyyy = simpleDateFormat_MMMddyyy.format(inputDate);
        String dateStr_DDDyyyy = simpleDateFormat_DDDyyyy.format(inputDate);

        System.out.println(dateStr_DDMMyyyy +" "+ dateStr_MMMddyyy+ " "+ dateStr_DDDyyyy);
    }
}
codiacTushki
  • 750
  • 1
  • 9
  • 22
  • I don't get this code at all. Does it let user input the date in all 3 formats? Does the result come out into 3 formats? – Jin Nguyen Mar 28 '15 at 00:16
  • I just show you how to convert String of various Date format to java.Date (in case of input) and vice-a versa (in case of output) . You can take input from user using System.in and parse that String to see the format of input and then using Switch logic you can pick the corresponding DateFormatter to convert that String to java.Date. When you want to output date to user in that case you can use DateFormatter.format with the specific DateFormat. – codiacTushki Mar 28 '15 at 08:56
  • Can you show me specific on the code please? Because I'm new and it's really hard to understand for me. Thanks! – Jin Nguyen Mar 28 '15 at 18:36
0

Use SimpleDateFormat to parse your input. your code should looke like this:

static final SimpleDateFormat MM_DD_YYYY = new SimpleDateFormat("MM/dd/yyyy");
static final SimpleDateFormat MMMM_DD_YYYY = new SimpleDateFormat("MMMM dd, yyyy",  Locale.US);
static final SimpleDateFormat D_YYYY = new SimpleDateFormat("D, yyyy");

Date parseFirstOption(String string) throws ParseException {
    return MM_DD_YYYY.parse(string);
}

Date parseSecondOption(String string) throws ParseException {
    return MMMM_DD_YYYY.parse(string);
}

Date parseThirdOption(String string) throws ParseException {
    return D_YYYY.parse(string);
}

void print(Date date) throws ParseException {
    System.out.println(MM_DD_YYYY.format(date));
    System.out.println(MMMM_DD_YYYY.format(date));
    System.out.println(D_YYYY.format(date));
}

Date date1 = parseFirstOption("06/15/2015");
Date date2 = parseSecondOption("June 15, 2015");
Date date3 = parseThirdOption("166, 2015");

print(date1);
  • Isn't this way hard coding? I want to do something with input. How can we do leapYear? – Jin Nguyen Mar 27 '15 at 20:19
  • You just hardcode the patterns, which of course you can put into some property file. The Date handles the leapyear by itself. Anyway the algorithm for leapyear is as follows: year has to be divisible by 4 but not divisible by 100 unless it is divisible by 400. – ToeKnee Mar 28 '15 at 22:10