How to convert string (Twitter4j format) to Java Date - java

I would like to extract date and year from the following string and convert it to a Data Object in Java.
Mon Jul 07 19:18:26 CEST 2014
How can I extract only date and year (in this case, 2014-07-07) from the text in a sophisticated way?

SimpleDateFormat s = new SimpleDateFormat("dd/MMM/yyyy");
String dateInString = "Mon Jul 07 19:18:26 CEST 2014";
SimpleDateFormat myFormat = new SimpleDateFormat("yyyy-MM-dd");
try {
Date date = s.parse(dateInString.split(" ")[2]+"/"+dateInString.split(" ")[1]+"/"+dateInString.split(" ")[5]);
System.out.println(new SimpleDateFormat("YYYY-MM-dd").format(date));
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
This should work for you, I splitted your String, then put it to a date format and then formatted it the way you wanted it to be, assumed you wanted the months as the second parameter after the year, if thats not the case you can simply change the 'MM' to 'dd' and the 'dd' to 'MM'.

For Java 7 or below, use a SimpleDateFormat for parsing and formatting:
Locale dateLocale = Locale.US;
SimpleDateFormat inFormat = new SimpleDateFormat("E MMM dd HH:mm:ss z yyyy", dateLocale);
Date date = inFormat.parse("Mon Jul 07 19:18:26 CEST 2014");
SimpleDateFormat outFormat = new SimpleDateFormat("yyyy-MM-dd", dateLocale);
System.out.println(outFormat.format(date));
Since Java 8, you can use DateTimeFormatter:
Locale dateLocale = Locale.US;
DateTimeFormatter inFormatter = DateTimeFormatter.ofPattern("E MMM dd HH:mm:ss z yyyy", dateLocale);
TemporalAccessor date = inFormatter.parse("Mon Jul 07 19:18:26 CEST 2014");
DateTimeFormatter outFormatter = DateTimeFormatter.ISO_LOCAL_DATE;
System.out.println(outFormatter.format(date));

Related

Format date with SimpleDateFormat Java [duplicate]

This question already has answers here:
Java String to Date, ParseException
(4 answers)
Closed 7 years ago.
I have a date string as
"Wed Jul 01 08:16:13 PDT 2015"
I am trying to parse it with this SimpleDateFormat
SimpleDateFormat valueDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
this way:
Date parsedDate1 = valueDateFormat.parse("Wed Jul 01 08:16:13 PDT 2015");
It is giving me parse error as:
java.text.ParseException: Unparseable date: "Wed Jul 01 08:16:13 PDT 2015" (at offset 0)
How can I get a date in above simple date format from the string
Try this:
DateFormat originalFormat = new SimpleDateFormat("EEE MMM dd hh:mm:ss zzz yyyy");
DateFormat targetFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
Date date = originalFormat.parse("Wed Jul 01 08:16:13 PDT 2015");
String formattedDate = targetFormat.format(date);
System.out.println(formattedDate);
I suspect you don't realy understand the consept of the SimpleDateFormat.
After you define the template with :
SimpleDateFormat valueDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
The valueDateFormat could parse Date object acording to it, it not take just a String you have and convert it. it take Date object.
Date
Your date string ("Wed Jul 01 08:16:13 PDT 2015") doesn't match your pattern ("yyyy-MM-dd hh:mm:ss")
Write correct pattern which matches date string (first goes Day of week, than month in year, etc.)
try this..
public static void main(String[] a)
{
SimpleDateFormat valueDateFormat = new SimpleDateFormat("EEE MMM yy hh:mm:ss");
try {
Date parsedDate1 = valueDateFormat.parse("Wed Jul 01 08:16:13 PDT 2015");
System.out.println(parsedDate1);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
For the formats like this I created a helper method:
public Date parseString(String date) {
String value = date.replaceFirst("\\D+([^\\)]+).+", "$1");
//Timezone could be either positive or negative
String[] timeComponents = value.split("[\\-\\+]");
long time = Long.parseLong(timeComponents[0]);
int timeZoneOffset = Integer.valueOf(timeComponents[1]) * 36000; // (("0100" / 100) * 3600 * 1000)
//If Timezone is negative
if(value.indexOf("-") > 0){
timeZoneOffset *= -1;
}
//Remember that time could be either positive or negative (ie: date before 1/1/1970)
//time += timeZoneOffset;
return new Date(time);
}
It returns date object from Date string so you can format your string like this:
Date date = parseString("Wed Jul 01 08:16:13 PDT 2015");
And after that you can easily format given date variable.

Trouble parsing Java Date Object?

I am receiving a Java Date formatted like so: "Sun Sep 14 02:00:00 PDT 2014" into a yyyy-MM-dd format but I can't seem to parse it. What I tried is the following:
String time = "Sun Sep 14 02:00:00 PDT 2014";
SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
Date contractEffectiveDateFormat = f.parse(time);
System.out.println("Date: " + contractEffectiveDateFormat);
However, I get an error saying that this date is unparsable. I'm not sure how to go about parsing this date because if I try to parse the date using the following:
DateFormat df = new SimpleDateFormat("EEE MMM dd kk:mm:ss z yyyy");
which is how to actually parse the date correctly into a Date object,
the string would turn into a Date object, but I can't seem to do anything with it from there. I want to turn it in so that it looks like 2014-09-14. Any ideas on how to do so? Thanks!
Use two DateFormat(s) one for input and for output,
String time = "Sun Sep 14 02:00:00 PDT 2014";
DateFormat out = new SimpleDateFormat("yyyy-MM-dd");
DateFormat in = new SimpleDateFormat("EEE MMM dd kk:mm:ss z yyyy");
try {
Date effectiveDate = in.parse(time);
System.out.println("Date: " + out.format(effectiveDate));
} catch (ParseException e) {
e.printStackTrace();
}
Output is the requested
Date: 2014-09-14
Your incoming string is this String time = "Sun Sep 14 02:00:00 PDT 2014";
which means the SimpleDateFormat pattern should match the incoming String pattern so you need to use SimpleDateFormat like this
DateFormat inFormat=new SimpleDateFormat("EEE MMM dd kk:mm:ss z yyyy",Locale.ENGLISH);
Then when you called parse() on inFormat it will give you Date Object which doesnot have particular format associated with it. So in order to format the Date again you need to create SimpleDateFormat object specifying the format you want which is this
DateFormat outFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
Ultimately bind all together
One more thing always specify the Locale
String time = "Sun Sep 14 02:00:00 PDT 2014";
DateFormat outFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
// good practice to specify the locale
DateFormat inFormat=new SimpleDateFormat("EEE MMM dd kk:mm:ss z yyyy",Locale.ENGLISH);
try {
Date parsedDate = inFormat.parse(time);
System.out.println("Required Formatted Date: " + outFormat.format(effectiveDate));
} catch (ParseException e) {
e.printStackTrace();
}
Simply add another SimpleDateFormat that'll allow you to present the Date object the way you want:
public static void main(String[] args) throws ParseException {
String time = "Sun Sep 14 02:00:00 PDT 2014";
DateFormat df = new SimpleDateFormat("EEE MMM dd kk:mm:ss z yyyy");
Date contractEffectiveDateFormat = df.parse(time);
System.out.println("Date: " + contractEffectiveDateFormat);
SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
System.out.println(f.format(contractEffectiveDateFormat)); // prints 2014-09-14
}

How can I convert a String to a Date and then to a different format?

Actually I wanted to convert one String to date and then I need to format it in to one another format..
The String I have is
String val="Wed Jan 08 08:49:13 GMT+05:30 2014";
To convert it to
2014-01-30 10:14:18 , this format
Is it possible to do this I tried some method like
SimpleDateFormat df = new SimpleDateFormat("dd-MMM-yyyy");
String formattedDate = df.format(c.getTime());
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT+1:00"));
// Date currentLocalTime = ;
DateFormat date = new SimpleDateFormat("HH:mm:ss:mmss a");
But nothing worked
Your format string for parsing doesn't match the example date you have given.
You could try something like the following:
String val = "Wed Jan 08 08:49:13 GMT+05:30 2014";
DateFormat inFmt = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy");
Date date = inFmt.parse(val);
DateFormat outFmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
outFmt.setTimeZone(TimeZone.getTimeZone("GMT+1:00"));
System.out.println(outFmt.format(date));
Note the following correspondences in the first two lines:
Wed -> EEE
Jan -> MMM
08 -> dd
etc.

Java. Cant parse date using DateFormat

I have a date string:
Thu Feb 20 08:00:00 EET 1992
And using this code to format it:
String datePatternFrom = "EEE MMM dd HH:mm:ss ZZZ yyyy";
String datePatternTo = "MMM dd, yyyy";
String prettyDate = "";
try {
DateFormat fromFormatter = new SimpleDateFormat(datePatternFrom);
Date date = (Date)fromFormatter.parse(userBirthday.toString());
DateFormat toFormatter = new SimpleDateFormat(datePatternTo);
prettyDate = toFormatter.format(date);
} catch (ParseException e) {
e.printStackTrace();
}
Why I'am getting the exception?
java.text.ParseException: Unparseable date: "Thu Feb 20 08:00:00 EET 1992" (at offset 0)
The problem is with the weekday and month and your locale.
Thu is English, so you have to tell the parser that it should use English weekdays:
DateFormat fromFormatter = new SimpleDateFormat(datePatternFrom, Locale.US);
This will work for your pattern.
If you do not specify a locale, the default will be used, which is not always an English one. ;-)
It could be your locale. Try making a SDF with datePatternFrom, give it a date to format and print that somewhere. See what pops up.
Probably your userBirthday object was not created as a java.util.Date object. Can you try a System.out.println(userBirthday.getClass().getName());?

convert string into java.util.date format in java

am having a string like this.
Thu Oct 07 11:31:50 IST 2010
I want to convert this into its exact date time format to store it in SQL.
Am familiar with so many string to date conversions like the following.
String dateString = "2001/03/09";
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/mm/dd");
Date convertedDate = dateFormat.parse(dateString);
But i need to convert a string like Thu Oct 07 11:31:50 IST 2010
into its Date format with timestamp.
Can anyone explain the proper way of converting this into its java.util.Date format.?
Try this:
SimpleDateFormat dateFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
For future reference read up on the SimpleDateFormat class:
http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
Use this format -'EEE MMM dd HH:mm:ss z yyyy'
Date date = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy")
.parse("Thu Oct 07 11:31:50 IST 2010");
System.out.println(date);
Can't you do like below
String str = "Thu Oct 07 11:31:50 IST 2010";
SimpleDateFormat sdf = new SimpleDateFormat("E MMM dd hh:mm:ss 'IST' yyyy");
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy/MM/dd");
System.out.println(sdf2.format(sdf.parse(str)));

Categories