-5

I'm trying to code an array of date objects called january, and fill with Jan 1, 2017 - Jan 31, 2017.

I have already created a class called Date and stored String months, int days, and int years. But I need a faster way to store all this data. Also, I was wondering if you could create a function called ToString() that returns a nicely formatted string containing the given date.

This is what I have so far:

Date[]january = new Date[31];
january[0].months = "January";
january[0].days = 1;
january[0].years =2017;
january[1].months = "January";
january[1].days = 2;
january[1].years =2017;
Hello
  • 11
  • 4
  • Java and JavaScript are two different languages. Which one are you asking a question about? Java? – Andy Sep 09 '17 at 20:52
  • @Andy Yes, java – Hello Sep 09 '17 at 20:53
  • 3
    Welcome to Stack Overflow! Please [take the tour](http://stackoverflow.com/tour) to see how the site works and what questions are on topic here, and edit your question accordingly. See also: [Why is "Can someone help me?" not an actual question?](http://meta.stackoverflow.com/q/284236) – Joe C Sep 09 '17 at 20:55
  • How do you determine that this is not fast enough? Or are you asking how to make your code more concise? Have you learnt loops yet? – JB Nizet Sep 09 '17 at 20:56
  • I want to use a for loop so that I don't have to write january[0] up to january[30] – Hello Sep 09 '17 at 20:57
  • So you know the solution. Why don't you apply it? – JB Nizet Sep 09 '17 at 20:58
  • I tried but I'm not really sure how to use it because I am new to coding – Hello Sep 09 '17 at 21:00
  • Added the answer you can check dear. – Charu Rajput Sep 09 '17 at 21:01
  • 5
    Programming is not like building bridges or houses. If you fail, the compiler or the runtime will tell you, and you get as many chanced as you want to fix your code, helped by the error messages. Read your text book, or the Java tutorial about for loops, and try something. There is no risk. – JB Nizet Sep 09 '17 at 21:04
  • how would you print this array? System.out.println(january[i]);? – Hello Sep 09 '17 at 21:05
  • @Hello I have edited my answer for your situation – Charu Rajput Sep 09 '17 at 21:18
  • Would it also be possible if you could create a function called ToString() that returns a nicely formatted string containing the given date – Hello Sep 09 '17 at 21:45
  • 1
    @Hello I've removed the "thank you" from the question, because the way to thank here is to [accept the answer that solved your problem and you found most helpful](https://stackoverflow.com/help/someone-answers) - and also upvote, when you have enough rep to do it. –  Sep 09 '17 at 22:23
  • 1
    You could fit your `Date` class with a three-argument constructor so that `new Date(2017, "January", 1)` would suffice to create a new object and assign the correct values to month, day and year. – Ole V.V. Sep 10 '17 at 06:03
  • 1
    It’s probably out of your scope for now, but I want to mention for anyone else coming around: once you learn about `enum`s, you will see that one is appropriate for the month rather than using strings. – Ole V.V. Sep 10 '17 at 06:05

3 Answers3

2

First of all, that code will crash. When you create a Date[31] you're creating an empty array, which will be filled with nulls. When you try to set a property on a null object, your program will crash. Before setting properties of january[0], you need to initialize it to new Date() (or whatever constructor is appropriate).

Instead of writing out all the days manually, you can use a for loop to generate them all.

Date[] january = new Date[31];
for (int i = 0; i < january.length; i++) {
    january[i] = new Date();
    january[i].months = "January";
    january[i].days = i + 1;
    january[i].years = 2017;
}

Note that when setting the days, 1 must be added to i because the array is zero-indexed but days of the month are not.

Edit: Here's my simple implementation of Date based on the code in the question:

public class Date {
    private String months;
    private int days;
    private int years;

    @Override
    public String toString() {
        return months + " " + days + ", " + years;
    }
}
Shadowfacts
  • 1,038
  • 10
  • 22
1

If this were real work rather than homework, I would:

  • Use the java.time objects for date-time work rather than make my own.
  • Use an ArrayList rather than a array.

The YearMonth class represents, well, the year-month.

Month is an enum, with a dozen pre-defined objects, one for each month of the year January-December.

A LocalDate is a date-only value, without time-of-day and without time zone.

List is an interface for an ordered collection, and ArrayList is a particular implementation.

YearMonth ym = YearMonth.of( 2017 , Month.JANUARY ) ;
int monthLength = ym.lengthOfMonth() ;
List< LocalDate > daysInJanuary = new ArrayList<>( monthLength ) ;
for( int i = 1 ; i <= monthLength ; i ++ ) {
    LocalDate ld = ym.atDay( i ) ;
    daysInJanuary.add( ld ) ;
}

daysInJanuary.toString(): [2017-01-01, 2017-01-02, 2017-01-03, 2017-01-04, 2017-01-05, 2017-01-06, 2017-01-07, 2017-01-08, 2017-01-09, 2017-01-10, 2017-01-11, 2017-01-12, 2017-01-13, 2017-01-14, 2017-01-15, 2017-01-16, 2017-01-17, 2017-01-18, 2017-01-19, 2017-01-20, 2017-01-21, 2017-01-22, 2017-01-23, 2017-01-24, 2017-01-25, 2017-01-26, 2017-01-27, 2017-01-28, 2017-01-29, 2017-01-30, 2017-01-31]

To generate a nicely formatted string representing each date's value, I would let java.time localize automatically.

Locale locale = Locale.CANADA_FRENCH ;  // Or Locale.US, etc.
DateTimeFormatter f = DateTimeFormatter.ofLocalizedDate( FormatStyle.FULL ).withLocale( locale ) ;

for( LocalDate ld : daysInJanuary ) {
    String output = ld.format( f ) ;
    System.out.println( output ) ;
}

Output is something like this, depending on the Locale:

Sunday, January 1, 2017

Monday, January 2, 2017

Tuesday, January 3, 2017

See this code run live at IdeOne.com. But be aware that IdeOne.com is limited to only a single Locale, Locale.US.

Community
  • 1
  • 1
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
-1
import java.util.*;
import java.text.SimpleDateFormat;

Date[]january = new Date[31];
for(int i=0;i<31;i++){ // or use january.length in place of 31
    january[i] = new Date(2017, 0, (i+1));
    System.out.println(formatDate(january[i]));
}

public static String formatDate(Date date){
       return (new SimpleDateFormat("MMMM").format(date) + " "+ new SimpleDateFormat("DD").format(date)  + ","+ date.getYear());
}

Use for loop or another loop because only date is changing in your code.

Charu Rajput
  • 653
  • 3
  • 15
  • 1
    That will throw a NullPointerException. – JB Nizet Sep 09 '17 at 20:58
  • 2
    january[i] is null. – JB Nizet Sep 09 '17 at 21:01
  • how would you print this array? System.out.println(january[i]);? – Hello Sep 09 '17 at 21:04
  • I have edited the answer. Please check. And output will be like Mon Jan 01 00:00:00 GMT 3917 Tue Jan 02 00:00:00 GMT 3917 Wed Jan 03 00:00:00 GMT 3917 Thu Jan 04 00:00:00 GMT 3917 Fri Jan 05 00:00:00 GMT 3917 Sat Jan 06 00:00:00 GMT 3917 Sun Jan 07 00:00:00 GMT 3917 Mon Jan 08 00:00:00 GMT 3917 Tue Jan 09 00:00:00 GMT 3917 Wed Jan 10 00:00:00 GMT 3917 Thu Jan 11 00:00:00 GMT 3917 Fri Jan 12 00:00:00 GMT 3917 Sat Jan 13 00:00:00 GMT 3917 Sun Jan 14 00:00:00 GMT 3917 Mon Jan 15 00:00:00 GMT 3917 Tue Jan 16 00:00:00 GMT 3917 and so on. – Charu Rajput Sep 09 '17 at 21:14
  • Also, you must use **import java.util.*;** because we are using Date – Charu Rajput Sep 09 '17 at 21:16
  • @Charu Have you noticed that this code prints the year **3917**? That's because you're using a deprecated ‘Date` constructor that [considers years indexed at 1900](https://stackoverflow.com/a/45880919/7605325), so 2017 becomes 3917. Strange and totally non-intuitive, probably that's why it's deprecated. –  Sep 09 '17 at 21:25
  • Yes i noticed that. I think for printing the output depends on user how he or she wants. For year we can use method getYear(). So it is his or her choice. User just want to escape from typing asked for for loop. So i just answered in that way. – Charu Rajput Sep 09 '17 at 21:27
  • Would it also be possible if you could create a function called ToString() that returns a nicely formatted string containing the given date? – Hello Sep 09 '17 at 21:46
  • Yes, see updated code now, also you have to write **import java.text.SimpleDateFormat** – Charu Rajput Sep 09 '17 at 22:02
  • Please don’t teach the young ones to use the long outdated classes `Date` and `SimpleDateFormat`. While for real-world code it’s certainly a good idea to use the standard library rather than growing your own class, today we have `java.time`, the modern Java date and time API, it’s so much better than the old classes. – Ole V.V. Sep 10 '17 at 06:00