Trouble parsing Java Date Object? - java

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
}

Related

Local Date to UTC conversion

I'm trying to convert system local date to UTC. Below is my code and it looks working for MST and EST formats. But, it is not working as expected.
String inputDate = "Wed Apr 13 04:00:00 IST 2022";
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
Date date = sdf.parse(inputDate);
DateFormat formatUTC = new SimpleDateFormat("MM/dd/yyyy");
formatUTC.setTimeZone(TimeZone.getTimeZone("UTC"));
String result = formatUTC.format(date);
System.out.print(result); // 04/13/2022
I see that IST zone is 5hrs 30mins ahead from the UTC universal time. So, I should get 04/12/2022 for the given input. But, getting 04/13/2022. what am I doing wrong here? Please advise.
Try setting the timezone for inputDate as well. Try the below code:
String inputDate = "Wed Apr 13 04:00:00 IST 2022";
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
sdf.setTimeZone(TimeZone.getTimeZone("Asia/Kolkata"));
Date date = sdf.parse(inputDate);
DateFormat formatUTC = new SimpleDateFormat("MM/dd/yyyy");
formatUTC.setTimeZone(TimeZone.getTimeZone("UTC"));
String result = formatUTC.format(date);
System.out.print(result);
Take a look at this answer.

How to convert string (Twitter4j format) to Java Date

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));

Convert string to date Android

I'm trying to convert a String that represents a date stored in SQLITE.
The date was stored into sqlite as follows:
Date date;
date.toString();
According with Java documentation, toString() method:
Returns a string representation of this Date. The formatting is
equivalent to using a SimpleDateFormat with the format string "EEE MMM
dd HH:mm:ss zzz yyyy", which looks something like "Tue Jun 22 13:07:00
PDT 1999". The current default time zone and locale are used. If you
need control over the time zone or locale, use SimpleDateFormat
instead.
Until here, it's fine but, when I try to get the String and convert it to date again, Java throws an exception.
The String comes from sqlite:
Mon Jan 20 18:26:25 BRT 2014
So, I do:
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US);
Date date= sdf.parse("Mon Jan 20 18:26:25 BRT 2014");
What I'm doing wrong?
Thanks.
try this code
String dateString = "here your date";
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
Date convertedDate = new Date();
try {
convertedDate = dateFormat.parse(dateString);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(convertedDate);
Try this:
String w = "Mon Jan 20 18:26:25 BRT 2014";
SimpleDateFormat pre = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
try{
Date date = pre.parse(w);
System.out.println(sdf.format(date));
}catch(Exception e){
e.printStackTrace();
}
Output:
20/01/2014
Formatter for storing and restoring data value in format dd/MM/yyyy
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd/MM/yyyy");
Storing data
String dataAsString = simpleDateFormat.format(date); // 20/01/2014
Restoring data
Date data = simpleDateFormat.parse(dataAsString);

Convert String into dateTime

I have to strings like this
Thu Oct 03 07:47:22 2013
Mon Jul 05 08:47:22 2013
I want to compare these dates, I am using SimpleDateFormat("EEE MMM dd HH:mm:ss yyy") but it gives me an exception : java.text.ParseException: Unparseable date:
Please help me to solve this problem!
You're missing an y for year:
EEE MMM dd HH:mm:ss yyyy
but you should use a more robust library, org.jodatime.
import org.joda.time.format.DateTimeFormat;
import org.joda.time.DateTime;
DateTimeFormat format = DateTimeFormat.forPattern("EEE MMM dd HH::mm:ss yyyy");
DateTime time = format.parseDateTime("Thu Oct 03 07:47:22 2013");
You missed a y in the format. 4 y were required for the year(though it may work fine with yyy, its better to use yyyy as it'll make your format more readable to others). And to get the DateTime object, you can use the Date object which you get by parsing the String to construct your DateTime.
Try something like this:-
String str = "Thu Oct 03 07:47:22 2013";
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss yyyy"); // You missed a y here.
try {
Date d = sdf.parse(str);
DateTime dt = new DateTime(d.getTime()); // Your DateTime Object.
} catch (ParseException e) {
// Parse Exception
}
try with this method
public static Date formatStringToDate(String strDate) throws ModuleException {
Date dtReturn = null;
if (strDate != null && !strDate.equals("")) {
int date = Integer.parseInt(strDate.substring(0, 2));
int month = Integer.parseInt(strDate.substring(3, 5));
int year = Integer.parseInt(strDate.substring(6, 10));
Calendar validDate = new GregorianCalendar(year, month - 1, date);
dtReturn = new Date(validDate.getTime().getTime());
}
return dtReturn;
}

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