Parse date with SimpleDateFormatter - java

Here's the code that should work, but does not:
public static void main(String[] args) {
String datata = "23:00:01 GMT, Sun Jul 28, 2012";
String format = "HH:mm:ss zzz, EEE MMM dd, yyyy";
try {
DateFormat inputFormat = new SimpleDateFormat(format);
Date parsedDate = inputFormat.parse(datata);
System.out.println(parsedDate.toGMTString());
} catch (Exception e) {
e.printStackTrace();
}
}
I'm getting a parse exception. I triple checked the patterns, I even wrote it one beneath the other, and I still get an exception. Help, anyone?

It is probably because your default locale is not in english and the parser does not understand "Sun" and/or "Jul". Try using:
DateFormat inputFormat = new SimpleDateFormat(format, Locale.ENGLISH);

Related

Android Convert Ruby on Rails Timestamp to Date Time

I am building an Android App, Fetching Data from API that is build in Ruby On Rails Framework.
Here is my timestamp string
2000-01-01T12:00:00.000Z
I need to convert it to Readable date and time format i.e "Saturday January 1st, 2000 12:00 am"
You can try this:
public static String convertDate(String oldDateString){
String format = "yyyy-MM-dd'T'HH:mm:ss.sssZZZZ";
SimpleDateFormat sdf = new SimpleDateFormat(format);
Date date = null;
try {
date = sdf.parse(oldDateString);
} catch (ParseException e) {
// handle exception here !
}
String newFormatString = "MMMM dd, yyyy 'at' HH:mm a";
SimpleDateFormat newFormatter = new SimpleDateFormat(newFormatString);
String newDateString = newFormatter.format(date);
return newDateString;
}
I had the same issue and I tried #real19 answer but it gave me errors.
I came with that then :
public static String convertDate(String oldDateString){
String format = "yyyy-MM-dd'T'HH:mm:ss.sss";
SimpleDateFormat sdf = new SimpleDateFormat(format);
Date date = null;
try {
date = sdf.parse(oldDateString);
} catch (ParseException e) {
// handle exception here !
e.printStackTrace();
}
String newFormatString = "EEEE MMMM dd, yyyy 'at' HH:mm a";
SimpleDateFormat newFormatter = new SimpleDateFormat(newFormatString);
String newDateString = newFormatter.format(date);
return newDateString;
}
Formatting gave me jeudi décembre 22, 2016 at 16:42 PM but you can adjust your DateFormat

DateTimePicker's format in java

I am using <sx:datetimepicker name="date1" label="From" displayFormat="yyyy-MM-dd"/> for picking the date. How ever I have to compare the picked date with date in mySQl table. The problem here is, the date in mySQl table is like 2014-09-29 20:36:25 and the format of piked date from struts2 tag is like Wed Dec 31 00:00:00 IST 2014. I am unable to compare both. Can any one tell me how to compare both? Or if I can change the format of picked time in java, how to do that?
You need to parse both Date to some same format so that you can compare that ..
see below code that can help you
public class Test {
public static void main(String[] args) {
SimpleDateFormat sdfForSturtsDate = new SimpleDateFormat("E MMM dd HH:mm:ss z yyyy");
SimpleDateFormat sdfForMySqlDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String DateFromSturts = "Wed Dec 31 00:00:00 IST 2014";
String DateFromMysql = "2014-09-29 20:36:25";
java.util.Date dateFromsturts= new java.util.Date();
java.util.Date dateFromMySql= new java.util.Date();
try {
dateFromsturts = sdfForSturtsDate.parse(DateFromSturts);
dateFromMySql = sdfForMySqlDate.parse(DateFromMysql);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// now you can compare dateFromsturts and dateFromMySql
}
}

converting string to date and converting date back to string

Hi for some of the requirement i need to convert the string representation of date(with no format) to date object and convert back to string(with a specific format)
This is what i tried so far, the output is not coming as expected and it's printing something like 08140009 - Any idea what is this
And please provide any suggestions.
MY code is :
public String getDateBackToCST(String createDate){
SimpleDateFormat dateFormatter = new SimpleDateFormat("MMddyyyy");
TimeZone obj = TimeZone.getTimeZone("CST");
dateFormatter.setTimeZone(obj);
Date createdDate = null;
try {
createdDate = dateFormatter.parse(createDate);
} catch (ParseException e) {
e.printStackTrace();
}
return dateFormatter.format(createdDate);
}
You need to specific proper flags for SimpleDateFormat. You have 2 options to specify timezone z and Z and to specify day name use E like this
import java.text.ParseException;
import java.text.SimpleDateFormat;
public class Main {
public static void main(String[] args) throws ParseException {
String date = "Sat Sep 20 23:39:04 IST 2014 ";
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd hh:mm:ss z yyyy");
System.out.println(sdf.parse(date));
}
}

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

Java - Date format with Turkish or other months

I want to format dates with month names with localized label in different languages including Turkish,
how do I set formatted labels for months
Use the SimpleDateFormat constructor taking a Locale.
SimpleDateFormat sdf = new SimpleDateFormat("dd MMMM yyyy", new Locale("tr"));
String date = sdf.format(new Date());
System.out.println(date); // 27 Eylül 2011
The Locale accepts an ISO-639-1 language code.
public static void main(String Args[]) {
String text = "Çar, 11 Eyl 2013 19:28:14 +03:00";
try {
Date sdf = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss", new Locale("tr")).parse(text);
SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String date = DATE_FORMAT.format(sdf);
System.out.println(date);
} catch (ParseException ex) {
Logger.getLogger(test.class.getName()).log(Level.SEVERE, null, ex);
}
}

Categories