Parsing Custom Date String to MySql Date String in Java - java

I have a 'Fri Jan 16 08:41:11 GMT 2015' datetime string in my database.
I would like to parse it as a mysql datetime format String.
I am trying the code below, but getting
java.text.ParseException: Unparseable date: "Fri Jan 16 08:41:11 GMT 2015".
Code is:
String date = null;
if(pos.get("utc") != null) {
try {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date parsed = sdf.parse("Fri Jan 16 08:41:11 GMT 2015");
date = sdf.format(parsed);
} catch (JSONException | ParseException e) {
e.printStackTrace();
}
}
Any suggestions would be appreciated.

Try this
SimpleDateFormat sdf = new SimpleDateFormat("E MMM dd HH:mm:ss z yyyy"); Reference

Thanks for the help :-)
String date = null;
if(pos.get("utc") != null) {
try {
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
Date parsed = sdf.parse("Fri Jan 16 08:41:11 GMT 2015");
date = sdf.format(parsed);
} catch (JSONException | ParseException e) {
e.printStackTrace();
}
}

Related

How can I reformat date and time?

How can I convert 24 hours time format into 12 hours format? I know this question has been asked many times, but my problem is different. My current time is:
Tue Nov 07 18:44:47 GMT+05:00 2017
I just want 6:44 pm from my date time. I tried this:
private void convertTime(String time)
{
try {
final SimpleDateFormat sdf = new SimpleDateFormat("H:mm");
final Date dateObj = sdf.parse(time);
System.out.println(dateObj);
System.out.println(new SimpleDateFormat("K:mm a").format(dateObj));
} catch (final ParseException e) {
e.printStackTrace();
}
}
final SimpleDateFormat sdf = new SimpleDateFormat("hh:mm");
Using hh will give you 12 hours format and HH 24 hour format. More details on documentation.
Edit:
Your initial format must be the following in order to parse your date string to a Date object:
final SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM d HH:mm:ss 'GMT'Z yyyy");
final Date dateObj = sdf.parse(time);
After that you can format time to your needs.
From SimpleDateFormat documentation:
"h:mm a": 12:08 PM
So the format you need for:
I just want 6:44 pm from my date time
is:
final SimpleDateFormat sdf1 = new SimpleDateFormat("EEE MMM d HH:mm:ss z yyyy");
Date date = sdf1.parse(time);
final SimpleDateFormat sdf = new SimpleDateFormat("h:mm a");
String newDateString = sdf.format(date);
try {
final SimpleDateFormat sdf = new SimpleDateFormat("hh:mm");
final Date dateObj = sdf.parse(time);
System.out.println(dateObj);
System.out.println(new SimpleDateFormat("K:mm a").format(dateObj));
} catch (final ParseException e) {
e.printStackTrace();
}

Parse date in the format '2014-09-03T13:13:08Z'

Can you please help me to parse the date in the format "2014-09-03T13:13:08Z",
I have tried the format
public static Date getFormatedDate(String date)
{
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss Z");
Date dateConverted = new Date();
try {
dateConverted = df.parse(date);
} catch ( ParseException e ) {
}
return dateConverted;
}
But it doesn't parse.
Your current date format works fine. You need to set Locale
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss Z",
Locale.ENGLISH);
eg:
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss Z", Locale.ENGLISH);
Date dateConverted = new Date();
try {
dateConverted = df.parse(date);
} catch (ParseException e) {
System.out.println("Error"+e);
}
System.out.println(dateConverted);
Out put:
Wed Oct 01 12:03:51 IST 2014

How to convert string like "Tue, 06 May 2014 15:27:23 +0000" to Java-Date?

I need to convert a string (e.g. "Tue, 06 May 2014 15:27:23 +0000") to java date to store in in a sqllite-database. I used the following code, but it does not work (ParseException):
public static Date formatDate(String theDate) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
Date date = null;
try {
date = formatter.parse(theDate);
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}
Do you have any ideas? Thanks for your help!
Best regards,
Robin
You are not using the correct format.
Try:
EEE, d MMM yyyy HH:mm:ss Z

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

How can i convert the date to aplhabetical readable format in java

I have a program in which i will get date in the below format "2015-01-17"
Please tell me how can i convert this data to this format JAN 17 2015 .
import java.text.SimpleDateFormat;
public class TestDate {
public static void main(String args[]) throws Exception
{
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd");
String s1 = "2015-01-17 ";
System.out.println("Result==> "+sdf1.format(sdf2.parse(s1)));
}
}
Change your sdf2 to use the MMM dd yyyy format instead...
String text = "2015-01-17";
try {
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat sdf2 = new SimpleDateFormat("MMM dd yyyy");
Date date = sdf1.parse(text);
System.out.println("Result==> " + sdf2.format(date));
} catch (ParseException exp) {
exp.printStackTrace();
}
Which outputs...
Result==> Jan 17 2015
try this way
SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
String dateInString = "2015-01-17";
try {
Date date = formatter.parse(dateInString);
System.out.println(date);
System.out.println(formatter.format(date));
} catch (ParseException e) {
e.printStackTrace();
}
According to the SimpleDateFormat documentation, you should be able to do that by parsing a date as follows: MMM dd yyyy.

Categories