4

I have string like this:

Mon, 14 May 2012 13:56:38 GMT

Now I just want only date i.e. 14 May 2012

What should I need to do for that?

Naveed
  • 41,517
  • 32
  • 98
  • 131
Neha
  • 245
  • 5
  • 17
  • are you looking to convert to a first-class `Date` object, or are you asking about how to strip the string (i.e., using a regex)? – andersoj May 15 '12 at 13:08
  • Looks like you need a [regular expression](http://docs.oracle.com/javase/tutorial/essential/regex/)... – Wernsey May 15 '12 at 13:08
  • yes i need a regex which will give me only date part – Neha May 15 '12 at 13:29

4 Answers4

8

The proper way to do it is to parse it into a Date object and format this date object the way you want.

DateFormat inputDF  = new SimpleDateFormat("EEE, d MMM yyyy H:m:s z");
DateFormat outputDF = new SimpleDateFormat("d MMM yyyy");

String input = "Mon, 14 May 2012 13:56:38 GMT";
Date date = inputDF.parse(input);
String output = outputDF.format(date);

System.out.println(output);

Output:

14 May 2012

This code is

  • easier to maintain (what if the output format changes slightly, while the input format is preserved? or vice versa?)
  • arguably easier to read

than any solution relying on splitting strings, substrings on fixed indexes or regular expressions.

aioobe
  • 413,195
  • 112
  • 811
  • 826
5

Why don't you parse() the String using DateFormat, which will give you a Date object. Give this Date to a Calendar, and you can query any of the fields that you want, such as get(Calendar.DAY_OF_MONTH).

Something like this...

SimpleDateFormat myDateFormat = new SimpleDateFormat("EEE, d MMM yyyy H:m:s z");
Date myDate = myDateFormat.parse(inputString);

Calendar myCalendar = Calendar.getInstance();
myCalendar.setTime(myDate);

String outputDate = myCalendar.get(Calendar.DAY_OF_MONTH) + " " + 
                    myCalendar.get(Calendar.MONTH) + " " +
                    myCalendar.get(Calendar.YEAR);

Might be a little bit lengthy to write, but at least you end up with a Calendar that can easily give you any field you want (if, for example, you want to perform some processing using one of the field values). It can also be used for calculations, or many other purposes. It really depends on what you want to do with the date after you have it.

wattostudios
  • 8,666
  • 13
  • 43
  • 57
  • -1, this doesn't compile. Even if it did, it would probably print a bunch of numbers, not for instance `"May"`. I would suggest using `outputDate = String.format("%1$te %1$tb %1$tY", myCalendar)` – aioobe May 15 '12 at 13:43
  • Cool, good suggestion, I wasn't aware of such a statement. As for it not compiling - there are a few things missing, like they would need to fill the `inputString` and do some imports - I can't do all the work for them. I was more about trying to present a different way to use the date which would allow them to use the data for something functional afterwards, if they needed that. I was aware of it printing `5` instead of `May` - I didn't want to overcomplicate the answer. – wattostudios May 15 '12 at 13:45
  • 1
    Also, Calendar.get returns an `int` which can't be combined with a char, `' '` and result in a `String` (which is what I was referring to actually). – aioobe May 15 '12 at 13:49
  • Thanks for pointing out my oversight - I have corrected this :-) – wattostudios May 15 '12 at 13:50
0

java.time

The accepted answer uses SimpleDateFormat which was the correct thing to do in 2012. In Mar 2014, the java.util date-time API and their formatting API, SimpleDateFormat were supplanted by the modern Date-Time API. Since then, it is highly recommended to stop using the legacy date-time API.

Solution using the modern date-time API:

import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

class Main {
    public static void main(String[] args) {
        String stdDateTime = "Mon, 14 May 2012 13:56:38 GMT";
        ZonedDateTime zdt = ZonedDateTime.parse(stdDateTime, DateTimeFormatter.RFC_1123_DATE_TIME);
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd MMM uuuu", Locale.ENGLISH);
        String strDate = zdt.format(formatter);
        System.out.println(strDate);
    }
}

Output:

14 May 2012

ONLINE DEMO

Learn more about the modern Date-Time API from Trail: Date Time.

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
-1

You can use this:

String st = "Mon, 14 May 2012 13:56:38 GMT";

for(int i=0;i<=st.length();i++){

  if (st.charAt(i)==':') {

  index=i;
  index=index-2;
   break;
  }


}

String onlyDate=st.substring(4,index).trim();