Converting MySQL Date to Java Date : Android - java

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

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

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 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.

convert string to date and format the date

I have String variable which contains date. Now how to display July 15 from that string variable? (for example 2012-07-15)
String strdate = "2012-07-15";
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MMM-dd");
try {
d1 = formatter.parse(strdate);
System.out.println(d1 + "---date first---");
} catch (ParseException e) {
e.printStackTrace();
}
Your parsing template does not match your example:
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
(with MM instead of MMM).
To output July 15, you can use a second formatter:
SimpleDateFormat output = new SimpleDateFormat("MMMM dd");
System.out.println(output.format(d1)); //July 15
Something like,
try {
String strdate = "2012-07-15";
DateFormat df1 = new SimpleDateFormat("yyyy-MM-dd"");
DateFormat df2 = new SimpleDateFormat("MMMM dd");
System.out.println(df2.format(df1.parse(strdate)+ "---date first---");
}
catch (ParseException e) {
// Exception handling
}

Convert string to Date in java

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

Categories