-2

I have a Date object created by new Date();

When I do

 String.valueOf(new Date()) 

I get

"Mon Jan 10 11:11:11 PST 2015"

Is there a way to format in the following way:

Monday Jan 10, 2015 at 5:15 PM

I tried DateFormat and all its examples but not able to get it in the format required.

Date curDate = new Date();

   System.out.println(curDate.toString());

        String DateToStr = DateFormat.getInstance().format(curDate); System.out.println(DateToStr);

        DateToStr = DateFormat.getTimeInstance().format(curDate); System.out.println(DateToStr);

        DateToStr = DateFormat.getDateInstance().format(curDate);             System.out.println(DateToStr);

        DateToStr = DateFormat.getDateTimeInstance().format(curDate);
        System.out.println(DateToStr);

        DateToStr = DateFormat.getTimeInstance(DateFormat.SHORT).format(curDate);
        System.out.println(DateToStr);

        DateToStr = DateFormat.getTimeInstance(DateFormat.MEDIUM).format(curDate);
        System.out.println(DateToStr);

        DateToStr = DateFormat.getTimeInstance(DateFormat.LONG).format(curDate);
        System.out.println(DateToStr);

        DateToStr=
DateFormat.getDateTimeInstance(DateFormat.MEDIUM,DateFormat.SHORT).format(curDate)
        System.out.println(DateToStr);
Nick Div
  • 5,338
  • 12
  • 65
  • 127
  • 2
    Well show what you've *tried* with `SimpleDateFormat` and the result, and we can help guide you. (It seems unlikely that a value which is 11:11:11 is going to produce 5:15 PM whatever you do, mind you...) – Jon Skeet Mar 10 '15 at 19:10

1 Answers1

1

If I understand your question, then yes. Using the pattern letters given in the documentation for SimpleDateFormat and something like

public static void main(String[] args) {
    String fmtString = "EEEE MMM d, yyyy 'at' h:mm a";
    // E - Day name in week 
    // M - Month in year
    // d - Day in month
    // y - Year
    // The constant String at
    // h - Hour in am/pm (1-12)
    // m - Minute in hour
    // a - Am/pm marker
    DateFormat sdf = new SimpleDateFormat(fmtString);
    try {
        Date d = sdf.parse("Monday Jan 10, 2015 at 5:15 PM");
        System.out.println(sdf.format(d));
    } catch (ParseException e) {
        e.printStackTrace();
    }
}

Note January 10, 2015 was a Saturday.

Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249