If I am given a timestamp value such as: Tue Jun 22 18:17:48 +0000 2010, how can I extract the values "Jun 22" from the timestamp and turn it into a variable?
-
Is the timestamp example coming from a String? – John Giotta Dec 08 '11 at 18:05
-
not initially but It can be converted to a string though – Edmund Rojas Dec 08 '11 at 18:06
-
@EdmundRojas see my post at http://stackoverflow.com/questions/8427169/converting-a-date-string-into-milliseconds-in-java/8427764#8427764 . It uses `Calendar` to find the correct date. – Phil Dec 08 '11 at 18:06
-
It seems that you have got a `java.util.Date`? For new readers to the question see if you can avoid that. The `Date` class is poorly designed and long outdated. Instead use `LocalDate` or another appropriate class from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. May 08 '21 at 00:22
4 Answers
you can use pure java code to get this
String input = "Tue Jun 22 18:17:48 +0000 2010";
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM d hh:mm:ss Z yyyy");
Date date = sdf.parse(input);
sdf = new SimpleDateFormat("MMM d");
System.out.println(sdf.format(date));
OUTPUT: Jun 22

- 18,414
- 7
- 41
- 58
-
I like this answer if you need to do additional processing on the date, but if you only need to display it the parsing might cause a performance hit and so you'd want to stick with simple strings. Depends on your use case. – cyber-monk Dec 08 '11 at 18:28
-
If what you want is the String "Jun 22" I would go with this answer. If you're looking to actually process the data, you should use a Calendar object and then `get()` the `MONTH` and `DATE` fields from it. Converting structured data into a string and then back into structured data should always be avoided. – Avi Cherry Dec 08 '11 at 19:31
-
@Avi Cherry can the timestamp be passed directly into that get object or must it be formatted first through another means? – Edmund Rojas Dec 08 '11 at 19:41
-
No need to format anything, you can set a Calendar object's value from a Date object directly. Typically you would call `Calendar myCal = Calendar.getInstance()` then `myCal.setTime(date)`. (I haven't a clue why there isn't a `getCalendar()` method that takes a Date as a param, though. – Avi Cherry Dec 08 '11 at 20:10
java.time
The idiomatic way to do it is by parsing the given date-time string into OffsetDateTime
(as it has a timezone offset) and format the resulting OffsetDateTime
into the String
of the desired pattern.
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
public class Main {
public static void main(String[] args) {
String strDateTime = "Tue Jun 22 18:17:48 +0000 2010";
DateTimeFormatter dtfInput = DateTimeFormatter.ofPattern("E MMM d H:m:s XX u", Locale.ENGLISH);
ZonedDateTime odt = ZonedDateTime.parse(strDateTime, dtfInput);
DateTimeFormatter dtfOutput = DateTimeFormatter.ofPattern("MMM dd", Locale.ENGLISH);
String result = dtfOutput.format(odt);
System.out.println(result);
}
}
Output:
Jun 22
Learn more about the the modern date-time API* from Trail: Date Time.
* For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.

- 71,965
- 6
- 74
- 110
If you just want it as a String, you can use:
String input = "Tue Jun 22 18:17:48 +0000 2010";
String monthAndDay = input.split("\\s+")[1] + input.split("\\s+")[2];
If it is coming as a date, check out my post from earlier today:
public class Example {
public static void main(String[] args) {
String input = "Tue Jun 22 18:17:48 +0000 2010";
String[] parts = input.trim().split(" ");
String monthAndDay = new StringBuilder()
.append(parts[1]).append(" ").append(parts[2]).toString();
System.out.println(monthAndDay);
}
}

- 5,470
- 6
- 33
- 42