0

How can I get day and date from given Strings. For example:

String date="25-12-2014";

How to get date and day from given string?

Expected output is,

 25
 Thu

I got stuck when I tried this.

  private static String getFormatedDate(String strDate) {
    String result = "";
    if(strDate != null) {
     if (strDate.contains("-")) {
         String[] dates = strDate.split("-");   
        for(int i=0;i<dates.length;i++) {
 result = result + Utils.replaceDateFormat(dates[i].trim(),"MMM dd", "EE, M.dd") + ("-");
        }
        int lastIndex = result.lastIndexOf("-");
        result = result.substring(0, lastIndex).trim();
     }
     else {
         result = Utils.replaceDateFormat(strDate.trim(),"MMM dd", "EE, M.dd");
     }
    }
    return result;
   }
  Utils:

  public static String replaceDateFormat(String value, String actualFormat, String exceptedFormat)                   {
    final int currentYear = Calendar.getInstance().get(Calendar.YEAR);
    final SimpleDateFormat fromDate = new SimpleDateFormat(actualFormat);
    final SimpleDateFormat toDate = new SimpleDateFormat(exceptedFormat);

    Date convertedFromDate = null;
    try {
        convertedFromDate = fromDate.parse(value);
    } catch (java.text.ParseException e) {
        e.printStackTrace();
    }

    final Calendar c1 = Calendar.getInstance();
    c1.setTime(convertedFromDate);
    c1.set(Calendar.YEAR, currentYear);     
    return toDate.format(c1.getTime());
}
rm-vanda
  • 3,122
  • 3
  • 23
  • 34
Shiv
  • 129
  • 3
  • 12

3 Answers3

2

Your methods are very convoluted for a relatively simple task. Why don't you use SimpleDateFormat? You can use the parse method. For example:

Date date = new SimpleDateFormat("dd-MM-yyyy").parse(string);

And then you can get the required fields from there.

EDIT

To get the day of the week, you were right with this code:

Date d = date.parse(result); 
Calendar c = Calendar.getInstance(); 
c.setTime(d); 
int day=c.get(Calendar.DAY_OF_WEEK);

And then if you want it in the format above, you could just make an array filled with the days of the week:

String[] daysOfWeek = new String[]{"Sun","Mon"... etc}
String day = daysOfWeek[day - 1];
Kyranstar
  • 1,650
  • 2
  • 14
  • 35
  • how to get day?Date d = date.parse(result); Calendar c = Calendar.getInstance(); c.setTime(d); int day=c.get(Calendar.DAY_OF_WEEK); when i use like this, i am getting 5 and not in day name. please help me to solve this. Thanks. – Shiv Jan 05 '15 at 18:51
  • Edited to answer your question – Kyranstar Jan 05 '15 at 18:55
  • 1
    The days are defined as followed: Sunday = 1 Monday = 2 Tuesday = 3 Wednesday = 4 Thursday = 5 Friday = 6 Saturday = 7 –  Jan 05 '15 at 18:58
  • 1
    Right - I just tested it. Will edit – Kyranstar Jan 05 '15 at 18:59
1

You can use the method from Calendar:

    String date = "25-12-2014";
    SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy");
    Calendar cal = Calendar.getInstance();
    cal.setTime(format.parse(date));
    int day = cal.get(Calendar.DAY_OF_MONTH);
    int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
    DateFormatSymbols symbols = new DateFormatSymbols(new Locale("en"));
    String[] days = symbols.getShortWeekdays();
    System.out.printf("%02d %3s\n", day, days[dayOfWeek]);

The symbols can be set to your Locale zone.

Nemolovich
  • 391
  • 2
  • 12
0

if you are allowed to use java 8 you can give LocalDate a chance:

String date = "25-12-2014";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy");
LocalDate ld = LocalDate.parse(date, formatter);
System.out.println(ld.getDayOfMonth() + ", " + ld.getDayOfWeek());

Output is:

25, THURSDAY

EDIT:

System.out.println(ld.getDayOfMonth() + ", " + ld.getDayOfWeek().substring(0, 3));

@No aNoNym suggestion is right, with the following you get

25, THU
Manu Zi
  • 2,300
  • 6
  • 33
  • 67