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();
}
}
}
Related
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
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
I have an android application that returns an Entry date Formatted like this 2014-08-26T16:23:30.803
I need it to display 08/16/2014 04:23 pm
Here is my code
items.add(new ListViewItem()
{{
Ticket = json_data.optString("TicketID");
Desc = json_data.getJSONObject("Location").optString("LocationName");
Status = json_data.optString("Status_Desc");
Poster = json_data.optString("Lastposter");
Time = json_data.optString("Entrydate");
}});
public static void main(String args[]) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
SimpleDateFormat sdp = new SimpleDateFormat("MM/dd/yyyy hh:mm a");
System.out.println(sdp.format(sdf.parse("2014-08-26T16:23:30.803")));
}
prints:
08/26/2014 04:23 PM
SimpleDateFormat sd = new SimpleDateFormat("MM'/'dd'/'yyyy hh:mm a");
sd.format(new Date());
SimpleDateFormat sdfDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss a");//dd/MM/yyyy
Date date= new Date("2009-09-22 16:47:08");
String strDate = sdfDate.format(date);
use this
SimpleDateFormat formatter = new SimpleDateFormat("dd/MMM/yyyy HH:mm a");
String dateInString = " 2014-08-26T16:23:30.803";
try {
Date date = formatter.parse(dateInString);
System.out.println(date);
System.out.println(formatter.format(date));
} catch (ParseException e) {
e.printStackTrace();
}
it helps you
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.
I'm trying to parse a string to a date field in an android application but I can't seem to get it correct. Here is the string I'm trying to convert to a date "03/26/2012 11:49:00 AM". The function I'm using is:
private Date ConvertToDate(String dateString){
SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss aa");
Date convertedDate = new Date();
try {
convertedDate = dateFormat.parse(dateString);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return convertedDate;
}
But I keep getting 3/1/112 11:49AM as the result.
You are wrong in the way you display the data I guess, because for me:
String dateString = "03/26/2012 11:49:00 AM";
SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss aa");
Date convertedDate = new Date();
try {
convertedDate = dateFormat.parse(dateString);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(convertedDate);
Prints:
Mon Mar 26 11:49:00 EEST 2012
it went OK when i used Locale.US parametre in SimpleDateFormat
String dateString = "15 May 2013 17:38:34 +0300";
System.out.println(dateString);
SimpleDateFormat dateFormat = new SimpleDateFormat("dd MMM yyyy HH:mm:ss Z", Locale.US);
DateFormat targetFormat = new SimpleDateFormat("dd MMM yyyy HH:mm", Locale.getDefault());
String formattedDate = null;
Date convertedDate = new Date();
try {
convertedDate = dateFormat.parse(dateString);
System.out.println(dateString);
formattedDate = targetFormat.format(convertedDate);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(convertedDate);
String str_date="13-09-2011";
DateFormat formatter ;
Date date ;
formatter = new SimpleDateFormat("dd-MM-yyyy");
date = (Date)formatter.parse(str_date);
System.out.println("Today is " +date.getTime());
Try this
This code will help you to make a result like FEB 17 20:49 .
String myTimestamp="2014/02/17 20:49";
SimpleDateFormat form = new SimpleDateFormat("yyyy/MM/dd HH:mm");
Date date = null;
Date time = null;
try
{
date = form.parse(myTimestamp);
time = new Date(myTimestamp);
SimpleDateFormat postFormater = new SimpleDateFormat("MMM dd");
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
String newDateStr = postFormater.format(date).toUpperCase();
String newTimeStr = sdf.format(time);
System.out.println("Date : "+newDateStr);
System.out.println("Time : "+newTimeStr);
}
catch (Exception e)
{
e.printStackTrace();
}
Result :
Date : FEB 17
Time : 20:49
GregorianCalendar date;
CharSequence dateForMart = android.text.format.DateFormat.format("yyyy-MM-dd", date);
Toast.makeText(LogmeanActivity.this,dateForMart,Toast.LENGTH_LONG).show();
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
String dateInString = "07/06/2013";
try {
Date date = formatter.parse(dateInString);
System.out.println(date);
System.out.println(formatter.format(date));
} catch (ParseException e) {
e.printStackTrace();
}
Output:
2014/08/06 16:06:54
2014/08/06 16:06:54