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
Related
My date string is "Oct 17 2016 12:17PM"
and My date parsing method is:
public static String formatDate(String dateString) {
String stringDate = "";
try {
//Mar 11 2016 2:21PM
if (dateString != null) {
SimpleDateFormat dt = new SimpleDateFormat("MMM dd yyyy hh:mma");
Date date = dt.parse(dateString);
// *** same for the format String below
SimpleDateFormat dt1 = new SimpleDateFormat("EEE , hh:mm a");
stringDate = dt1.format(date);
}
return stringDate;
} catch (ParseException parseException) {
Log.e("date format", "" + parseException.getMessage());
}
return stringDate;
}
It is working fine with english language but when i change my app language to hindi or any other i am getting an exception.
java.text.ParseException: Unparseable date: "Oct 17 2016 12:17PM" (at offset 0)
What i do?
You need set Locale English
SimpleDateFormat dt = new SimpleDateFormat("MMM dd yyyy hh:mma", Locale.ENGLISH);
public class Practice {
public static void main(String...args) {
String day,month, finalDay;
try {
DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
String currentDateTimeString = df.format(new Date());
//String currentDateTimeString = DateFormat.getDateTimeInstance().format(new Date());
SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy", Locale.US);
Date date = format.parse(currentDateTimeString);
SimpleDateFormat dayFormat = new SimpleDateFormat("dd");
day = dayFormat.format(date);
SimpleDateFormat monthFormat = new SimpleDateFormat("MM");
month = monthFormat.format(date);
System.out.print("My day"+ day);
System.out.println("My month"+ month);
} catch (Exception e) {
System.out.println("Hello exception "+ e.getMessage());
e.printStackTrace();
}
}
}
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
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();
}
}
I get a value of
2014-04-13
from mysql.
I used this code
String date = "2014-04-13";
try {
SimpleDateFormat formatter = new SimpleDateFormat("dd MMM, yyyy", java.util.Locale.getDefault());
Date convertedDate = (Date) formatter.parse(date);
Log.d("Date", convertedDate.toString());
} catch (ParseException e) {
e.printStackTrace();
}
the conversion wont happen. it gives a parse exception and also the android app gets stopped.
I Want the output as
04 April, 2014
I got it working
try {
SimpleDateFormat readFormat = new SimpleDateFormat( "yyyy-MM-dd", java.util.Locale.getDefault());
SimpleDateFormat writeFormat = new SimpleDateFormat( "dd MMM, yyyy", java.util.Locale.getDefault());
java.util.Date convertedDate = readFormat.parse( date );
String formattedDate = writeFormat.format( convertedDate );
Log.d("Date", formattedDate);
} catch (ParseException e) {
e.printStackTrace();
} }
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.