-4
package pass;
import java.lang.*;

public class Date{
    private Mois mois;

    public String date(String d){
        int year=Integer.parseInt(d.substring(0,4));
        int mount=Integer.parseInt(d.substring(5,7));
        int day=Integer.parseInt(d.substring(8));
        moi = Mois(mount);
        String mois=moi.toString();
        return ""+mois+" "+day+", "+year;


    }
    public enum Mois{
        January(1),
        February(2),
        March(3),
        April(4),
        May(5),
        June(6),
        July(7),
        August(8),
        September(9),
        October(10),
        November(11),
        December(12);

    private int mois;

    public Mois(int k){
        this.mois=k;
    }
    public String toString(){
        return this.toString();
    }

    }
}

I need a java code return a string date

I cant use Date format ,, only string

input "2017-01-15" String should return "January 15, 2017" String?

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • What have you already tried? – Egor Zhuk Feb 22 '17 at 17:13
  • I know that linking outside is frowned upon, but really, a 2 second Google search tells me this: https://www.tutorialspoint.com/java/java_date_time.htm – durbnpoisn Feb 22 '17 at 17:14
  • This has been answered *many* times already. Please search Stack Overflow before posting. – Basil Bourque Feb 22 '17 at 17:15
  • @EgorZhuk i edited my question – Mohamd El-Saleh Feb 22 '17 at 17:16
  • @durbnpoisn input "2017-01-15" String should return "January 15, 2017" String true but i need a string input and string output – Mohamd El-Saleh Feb 22 '17 at 17:18
  • I need it without Date format @durbnpoisn – Mohamd El-Saleh Feb 22 '17 at 17:19
  • 1
    The requirement not to use a date formatter is a rather critical part of your question. Please update your question to say exactly what you are allowed to use and what problem you have with your existing code. – greg-449 Feb 22 '17 at 17:23
  • 1
    @MohamdEl-Saleh, nice to see that you have put a real effort into your question this time. Next things to learn may be to detail your constraints in the question, and maybe more importantly, when asking for debugging help, to specify the exact problem or error you have with your code. Quote your error message precisely in the question, and it will but much easier to understand what you’re really asking about and much easier to guide you. It means more than you think. :-) – Ole V.V. Feb 22 '17 at 18:56

2 Answers2

0

You need to use SimpleDateFormat and specify format in it.

Conversion using today's date:

Format formatter = new SimpleDateFormat("MMMM dd, yyyy"); 
String s = formatter.format(new Date());
System.out.println(s); 

Output

February 22, 2017 

Conversion from String date:

String date="2017-01-15";
DateFormat formatter1 = new SimpleDateFormat("yyyy-MM-dd"); 
Format formatter2 = new SimpleDateFormat("MMMM dd, yyyy"); 
String t = formatter2.format(formatter1.parse(date));
System.out.println(t);

Output

January 15, 2017

Formatting using Java 8

    String date="2017-01-15";
    DateTimeFormatter formatter1 = DateTimeFormatter.ofPattern("yyyy-MM-dd"); 
    LocalDate ldate = LocalDate.parse(date, formatter1);
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MMMM dd, yy"); 
    String s = ldate.format(formatter);
    System.out.println(s);
Atul
  • 1,536
  • 3
  • 21
  • 37
0

As you may have guessed, your problem is with trying to create an enum constant outside the enum through Mois(mount). An enum guarantees that there is always exactly 1 instance of each constant in your JVM, so you cannot do that.

Fortunately, in your case there is an easy way around it. Your months are numbered 1 through 12. At the same time, all the constants in an enum are already numbered, only from 0 (so through 11 in this case). So to get Mois number mount, just do:

    Mois moi = Mois.values()[mount - 1];

values() returns an array containing all the constants. I subtract 1 from mount to convert from your 1-based numbering to Java’s 0-based.

There are a few minor issues in your code too. Better fix them to get the thing working:

  • You cannot do public Mois(int k) in an enum; your IDE probably already told you. An enum constructor must be private. I understand why you tried public, but with the fix above we no longer need that, we can just declare it private.
  • Your toString method calls itself recursively leading to an infinite recursion. Just delete the method. enums have default toString methods that do as you expect.
  • You have two declarations of a variable named mois. Just delete the instance variable private Mois mois;. Again, with the above fix you don’t need it.

Now when I call date("2017-01-15"), I get January 15, 2017.

I understand that you are not allowed to use standard classes, so I suggest that the above is your answer. For anyone else, in the real world one would probably want to use Java’s standard classes for date manupulation. Since Java 8, one should do something like:

    String output = LocalDate.parse(inputDateString, DateTimeFormatter.ofPattern("uuuu-MM-dd", Locale.ROOT))
            .format(DateTimeFormatter.ofPattern("MMMM d, yyyy", Locale.ROOT));

Choose an appropriate locale (or leave out the two locale arguments to use the computer’s defaut). If you wanted day-of-month to have prefixed zero for days 1 through 9 (01 through 09), you would use dd in the second formatting pattern.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161