So I call this method:
System.out.println(getTwitterDate("Thu, 3 Jan 2010 18:26:07 +0000").getMonth());
And I keep getting 11 for every month I use.
I'm doing this in the getTwitterDate:
final String TWITTER="EEE, dd MMM YYYY HH:mm:ss ZZZZZ";
SimpleDateFormat sf = new SimpleDateFormat(TWITTER);
return sf.parse(date);
I'm going by this https://stackoverflow.com/a/4522095/1193534 but removed the sf.lenient part.
What am I doing wrong?
Just use small y instead of Y:
final String TWITTER="EEE, dd MMM yyyy HH:mm:ss ZZZZZ";
This is the latest created_at format received from the Twitter response for GET lists/statuses.
"EEE MMM dd HH:mm:ss ZZZZZ yyyy"
Related
I receive a date from a file in this format: "EEE MMM dd hh:mm:ss z yyyy" and I'm trying to convert this value into Date "yyyy-MM-dd". For that I'm using:
TalendDate.parseDate("yyyy/MM/dd", TalendDate.formatDate("yyyy/MM/dd", TalendDate.parseDate("EEE MMM dd hh:mm:ss z yyyy",context.date)))
The context.date is defined here:
context.date = input_row.mtime_string;
But when I run my JavaRow component I get the following error:
Exception in component tJavaRow_1
"java.lang.RuntimeException: java.text.ParseException: Unparseable date: "Thu Aug 09 10:38:45 BST 2018"
How can I solve this?
Many Thanks!
You could achieve the format using the below code snippet -
System.out.println(input_row.newColumn);
SimpleDateFormat parserSDF = new SimpleDateFormat("EEE MMM dd hh:mm:ss z yyyy", Locale.ENGLISH);
Date date = parserSDF.parse(input_row.newColumn);
String dDate = null;
parserSDF = new SimpleDateFormat("yyyy-MM-dd");
dDate = parserSDF.format(date);
System.out.println(dDate);
Also, you need the below libraries to be imported(Advanced settings section of tJavaRow)-
import java.text.SimpleDateFormat;
import java.util.Locale;
import java.util.Date;
I had directly passed the input value from file(in my scenario tFileInputDelimited) into tJavaRow as - input_row.newColumn and then used SimpleDateFormat class to both parse and format dates according to the formatting pattern.
Read more here.
If you want to convert the date into LocalDate then below code might help:
private LocalDate getLocalDate(String date){
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("EEE MMM dd hh:mm:ss z yyyy", Locale.getDefault());
return LocalDate.parse(date, formatter);
}
And once you get the LocalDate, you can transform it to any format. As the question expects yyyy-MM-dd then just call toString() on LocalDate object.
LocalDate curr = LocalDate.now();
System.out.println(curr.toString());
It will display the date like "2019-11-20".
Hope this helps to someone.
I am getting the above error, but to me everything seems to be correct.
What I am doing wrong?
DateTimeFormatter simpleDateFormatInput= DateTimeFormat.forPattern("EEE, dd MMM yyyy HH:mm:ss Z");
DateTime datetime = simpleDateFormatInput.parseDateTime(pubDate);
Where pubDate is Sat, 30 Jan 2016 12:23:53 +0100
The day and/or month from your input String may not match those from your default Locale. Try
DateTimeFormatter simpleDateFormatInput =
DateTimeFormat.forPattern("EEE, dd MMM yyyy HH:mm:ss Z").withLocale(Locale.US);
I have dates in following formats
EEEE MMM dd, yyyy HH:mm aaa
EEE, dd MMM yyyy HH:mm:ss z
EEE, MMMM dd, yyyy, HH:mm aaa z
yyyy-MM-dd-HH-mm
yyyyMMddHHmm
EEEE, MMMM dd, yyyy HH:mm aa
yyyyMMddHHmmssSS
yyyyMMddHHmmss
MM/dd/yyyy HH:mm
I want these dates to be in my local GMT, and convert from one GMT to other GMT dynamically in java.
Is there any native java function to this or i have to code it.
e.g convert 11/07/2013 12:32pm (MM/dd/yyyy HH:mm) to GMT+5 timezone
i mean to say that if my datetime is in GMT-10 timezone, that what will be its value in GMT+5 timezone
Any help will be appreciated. Thanks
private String converttogmt(Date date){
SimpleDateFormat datef = new SimpleDateFormat("yyyy-MM-dd-HH-mm");
datef.setTimeZone(TimeZone.getTimeZone("GMT"));
return datef.format(date);
}
Try this one.
I have the following code to pase a date
DateFormat df = new SimpleDateFormat("EEE, dd MMM yyyy kk:mm:ss", Locale.getDefault());
Date _pubDate = df.parse(_pubDateE.getFirstChild().getNodeValue());
But I get this error:
java.text.ParseException: Unparseable date: "Fri, 12 Aug 2011 15:34:47 CEST"
What is wrong ?
You're missing the timezone in the date format at the end, in your exception message, the "CEST" part.
Your code
DateFormat df = new SimpleDateFormat("EEE, dd MMM yyyy kk:mm:ss", Locale.getDefault());
should be
DateFormat df = new SimpleDateFormat("EEE, dd MMM yyyy kk:mm:ss z", Locale.getDefault());
You might want to read SimpleDateFormat
Edit
At the bottom of this page, the timezone format is more cleary explained
Clearer Timezone format
I think you need to add zzz in the end (for timezone):
"EEE, dd MMM yyyy kk:mm:ss zzz"
The date string is not in the format you specified. Notice the time zone at the end?
You probably want: new SimpleDateFormat("EEE, dd MMM yyyy kk:mm:ss zzz", Locale.getDefault());
How can I parse a pubDate from a RSS feed to a Date object in java.
The format in the RSS feed:
Sat, 24 Apr 2010 14:01:00 GMT
What I have at the moment:
DateFormat dateFormat = DateFormat.getInstance();
Date pubDate = dateFormat.parse(item.getPubDate().getText());
But this code throws an ParseException with the message Unparseable date
You can define the date format you are trying to parse, using the class SimpleDateFormat:
DateFormat formatter = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz");
Date date = formatter.parse("Sat, 24 Apr 2010 14:01:00 GMT");
Additionally, for non-English Locale's, be sure to use the following when parsing dates in English:
new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz", Locale.ENGLISH);
If you need to have an RFC822 compliant date, try this :
DateFormat dateFormatterRssPubDate = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z", Locale.ENGLISH);
For the lucky one that can use the Java 8 LocalDateTime:
LocalDateTime localDateTime = LocalDateTime.from(DateTimeFormatter.RFC_1123_DATE_TIME.parse("Sat, 24 Apr 2010 14:01:00 GMT"));