Convert string to Date in java - 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

Related

Date Parse Exception in java java.text.ParseException

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 getting wrong timestamp and my date formatter is right

String RollStart_TIMESTAMP = RollStartDATE_TIME_STAMP+" "+rolStart_TIME_STAMP;
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-mm-yyyy hh:mm");
Date parsedDate = null;
try {
parsedDate = dateFormat.parse(RollStart_TIMESTAMP);
long mili = parsedDate.getTime();
Log.e("VX:","TIMESTAMP"+mili);
} catch (ParseException e) {
e.printStackTrace();
}
Log.e("VX:","TIMESTAMP"+parsedDate);
Try this code, You get a right time stamp.
SimpleDateFormat dateFormat = new SimpleDateFormat("MMMM dd,yyyy HH:mm aaa");
timestamp = dateFormat.format(new Date()); // Find todays date
Log.e("Timestamp", timestamp);
Yes right there is wrong pattern yyyy-MM-hh is helps me out .
try {
String RollStart_TIMESTAMP = RollStartDATE_TIME_STAMP+" "+rolStart_TIME_STAMP;
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
Date timestamp = null;
timestamp = df.parse(RollStart_TIMESTAMP);
long ROLL_START_DATE_TIME_VALUE = timestamp.getTime()/1000;
Log.e("RX:","VALUE"+ROLL_START_DATE_TIME_VALUE);
} catch (Exception e) {
e.printStackTrace();

Converting MySQL Date to Java Date : Android

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

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
}

Categories