Date format issue : Convert from String to Date - java

All,
I need to convert the following string "Tue Jan 01 08:00:00 CET 2013" to a date object with format "dd/MM/yyyy HH:mm".
What I have done till now...
String dateStr = "Tue Jan 01 08:00:00 CET 2013";
DateFormat readFormat = new SimpleDateFormat("E MMM dd HH:mm:ss Z yyyy");
DateFormat writeFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm");
Date date = null;
try
{
date = readFormat.parse(dateStr);
} catch ( ParseException e )
{
e.printStackTrace();
}
String formattedDate = "";
if( date != null )
{
formattedDate = writeFormat.format(date);
}
System.out.println(formattedDate);
But this gives me a String as a result and not a date. If I parse the formattedDate String again using the writeFormat then I get the same original date back again i.e. Tue Jan 01 08:00:00 CET 2013.
NOTE: Finally, I want to push the date into MySQL DateTime datatype via Java Date object. i.e. String -> Java Date -> MySQL Date Time.
I have searched High/Low on the web and could not find a proper solution. Please help!!!
Thanks and regards,
SG

I found the answer.
DateFormat readFormat = new SimpleDateFormat("E MMM dd HH:mm:ss Z yyyy");
date = readFormat.parse(string);
And then while saving to MYSQL database...
pstmt.setDate(5, new java.sql.Date(change.getEndDateTime().getTime()));
Thanks for all your responses and apologies for posting duplicate if it is...
Regards,
SG

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.

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.

i want to get day name , day , month ,year from the date stored in String like "Wed, 27 feb 2013"

"JAVA" i stores Date in String like "wed, 27 Dec 2013" i want to extract day name , day , month, year from String date i am using spring MVC ,please guide me if anyone know solution . or if there are other way then also please suggest me ..thanks in advance.
This will do it. No JODA dependency:
DateFormat formatter = new SimpleDateFormat("EEE, dd MMM yyyy");
formatter.setLenient(false);
String dateStr = "Fri, 27 Dec 2013";
Date date = formatter.parse(dateStr);
Calendar calendar = Calendar.instance();
calendar.setTime(date);
// Get values from calendar.
DateFormat formatter = new SimpleDateFormat("EEE, dd MMM yyyy");
String dateStr = "WED, 27 Dec 2013";
Date date = formatter.parse(dateStr);
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
// Get values from calendar.
System.out.println(calendar.get(Calendar.DAY_OF_WEEK));//1 indexed starting from Sunday
System.out.println(calendar.get(Calendar.DAY_OF_MONTH));//0 indexed
System.out.println(calendar.get(Calendar.MONTH));//0 indexed
System.out.println(calendar.get(Calendar.YEAR));
Take a look at Joda time. It provides lots of methods for parsing dates, and obtaining data from them once they have been parsed.
Since the day of the week is inconsistent with the date represented, we will cut the string, and remove the day of the week, using substring().
public static void main(String argsp[]) {
DateFormat formatter = new SimpleDateFormat("dd MMM yyyy");
formatter.setLenient(false);
String dateStr = "Wed, 27 Dec 2013";
String sub = dateStr.substring(4);
try {
Date date = formatter.parse(sub);
System.out.println(date);
} catch (Exception e) {
e.printStackTrace();
}
}

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

Categories