I am developing an app in which i want to display the date and time the code works fine in other devices except in motox.I don't know where the problem is.
public static String convertDateTimeForm(Context context,String date){
String date_tim=date;
//date_tim=date.toString().replaceAll("\\.","");
//date_tim=date_tim.replaceAll("AM","am").replaceAll("PM","pm");
String newTime="";
try{
SimpleDateFormat dateFormat=null;
dateFormat= new SimpleDateFormat("yyyy-MMM-dd hh:mm a");
simpleDateFormat convertFormat;
convertFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date newDate=null;
newDate=dateFormat.parse(date_tim);
newTime=convertFormat.format(newDate);
}catch(Exception e){
e.printStackTrace();
}
return newTime;
}
Related
I am making a date filter for which I have created a custom method for date to be parse in specific date format.
I have date with to two formats dd MMM yyyy & yyyy-mm-dd which is passed in a single method to be parse and return in format of yyyy-mm-dd. As I have a complex structure at end both type of formatted string will go under the date parsing method.
ISSUE:: I am getting a blank string as return from this method when format is in yyyy-mm-dd. please provide me inputs of where i am wrong. Below is the code
//fetching date from methods
String current_date=CurrentFilterPeriod.dateParsing("2017-04-02");
String prev_date=CurrentFilterPeriod.dateParsing("01 Apr 2017");
//singleton file for date filter method
public class CurrentFilterPeriod {
private static Calendar cal = getInstance();
private static Date current_date = cal.getTime();
//defined formats for date
private static SimpleDateFormat formatter = new SimpleDateFormat("dd MMM yyyy");
private static SimpleDateFormat formatterString = new SimpleDateFormat("yyyy-MM-dd");
//method for parsing date
public static String dateParsing(String date){
Date newDate;
String returnDate = "";
if (date.equals(formatter.toPattern())){
returnDate=date;
}
Log.e("DB","date===>"+date);
try {
newDate = formatter.parse(date);
Log.e("DB","New Date===>"+newDate);
returnDate=formatterString.format(newDate);
Log.e("DB","returnDate===>"+returnDate);
} catch (ParseException e) {
e.printStackTrace();
}
return returnDate;
}
}
RESULT:: current_date="" prev_date="2017-04-01"
I am stuck here please help me or tell me other methods to get by desired output.Want result in format of yyyy-mm-dd
As you want result format like: yyyy-mm-dd. You need to check your Date String with formatterString formatter.
Change your code with:
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
boolean isValidDate(String input) {
try {
format.parse(input);
return true;
}
catch(ParseException e){
return false;
}
}
Now call the method using:
//method for parsing date
public static String dateParsing(String date) {
Date newDate;
String returnDate = "";
if (isValidDate(date)) {
returnDate = date;
return returnDate;
} else {
Log.e("DB", "date===>" + date);
try {
newDate = formatter.parse(date);
Log.e("DB", "New Date===>" + newDate);
returnDate = formatterString.format(newDate);
Log.e("DB", "returnDate===>" + returnDate);
} catch (ParseException e) {
e.printStackTrace();
}
}
return returnDate;
}
I have a problem with parsing a string to sql.date
This code works in my project only the first time, it will parse the date normally, but second time it throws exception.
I printed the date the function receives and it is the same format, for example 02.02.2016 was okey, I only changed month to 02.04.2016 and the exception was raised.
private final SimpleDateFormat dateFormat = new SimpleDateFormat("dd.mm.yyyy");
private final String sqldateFormat = "yyyy-mm-dd";
public java.sql.Date changeDate(String date) {
String newDate = "";
try {
java.util.Date d = dateFormat.parse(date);
dateFormat.applyPattern(sqldateFormat);
newDate = dateFormat.format(d);
} catch (ParseException e) {
e.printStackTrace();
}
return java.sql.Date.valueOf(newDate);
}
Try this
private final SimpleDateFormat dateFormat = new SimpleDateFormat("dd.mm.yyyy");
private final SimpleDateFormat sqldateFormat = new SimpleDateFormat("yyyy-mm-dd");
public java.sql.Date changeDate(String date) {
String newDate = "";
try {
java.util.Date d = dateFormat.parse(date);
newDate = sqldateFormat.format(d);
} catch (ParseException e) {
e.printStackTrace();
}
return java.sql.Date.valueOf(newDate);
}
Because during the fisrt execution you are modifying the pattern of the SimpleDateFormat it won't be able to parse the second date.
dateFormat.applyPattern(sqldateFormat); will modify the pattern to "yyyy-mm-dd" and then parsing 02.04.2016 will throw an exception.
this is because you change pattern of dateFormat.
This will work:
private final SimpleDateFormat dateFormat = new SimpleDateFormat("dd.mm.yyyy");
private final SimpleDateFormat sqlFormat = new SimpleDateFormat("yyyy-mm-dd");
public java.sql.Date changeDate(String date) {
String newDate = "";
try {
java.util.Date d = dateFormat.parse(date);
newDate = sqlFormat.format(d);
} catch (Exception e) {
e.printStackTrace();
}
return java.sql.Date.valueOf(newDate);
}
Apparently, this will work for the first run, but not for the second. Your problem is that you call applyPattern(), so it'll expect the new dates in sql date format only.
Here is a little better code:
private final SimpleDateFormat dateFormat = new SimpleDateFormat("dd.mm.yyyy");
private final SimpleDateFormat sqlFormat = new SimpleDateFormat("yyyy-mm-dd");
public java.sql.Date changeDate(String date) {
String newDate = "";
try {
java.util.Date d = dateFormat.parse(date);
newDate = sqlFormat.format(d);
} catch (ParseException e) {
e.printStackTrace();
}
return java.sql.Date.valueOf(newDate);
}
Don't use valueOf().
If you have a java.util.Date and want a java.sql.Date (or java.sql.Timestamp), use the Date(long date) constructor:
java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());
Also, don't catch exceptions and continue execution without handling it (printing it is not handling it).
Meaning that your code should be:
private final SimpleDateFormat dateFormat = new SimpleDateFormat("dd.mm.yyyy");
public java.sql.Date changeDate(String date) {
try {
return new java.sql.Date(dateFormat.parse(date).getTime());
} catch (ParseException e) {
throw new IllegalArgumentException("Invalid date: " + date);
}
}
Warning: SimpleDateFormat is not thread-safe:
Date formats are not synchronized. It is recommended to create separate format instances for each thread. If multiple threads access a format concurrently, it must be synchronized externally.
I am using the following code to format date.
private String getDate(String datestring) {
Date date = null;
DateFormat writeFormat = new SimpleDateFormat("dd-MM-yyyy");
try {
date = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy",
Locale.ENGLISH).parse(datestring);
String formattedDate = "";
if (date != null) {
formattedDate = writeFormat.format(date);
}
Log.d("Complaint adapter", formattedDate);
return formattedDate;
} catch (ParseException e) {
e.printStackTrace();
}
return "";
}
This code works fine in some devices, but when run this same in Micromax A111, it returns nothing. That is not displayed in the given field. Can anyone help me to solve this problem.
Finally the following code worked for me.
private String getDate(String datestring) {
// TODO Auto-generated method stub
SimpleDateFormat currentformat = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", java.util.Locale.getDefault());
SimpleDateFormat requiredformat = new SimpleDateFormat("dd-MM-yyyy", java.util.Locale.getDefault());
try {
Date date = null;
date = currentformat.parse(datestring);
datestring = requiredformat.format(date);
return datestring;
} catch (ParseException e) {
Log.e("Formating date", e.getMessage());
}
return datestring;
}
public class DateCreation {
public static void main(String[] args) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date = new Date();
String format = sdf.format(date);
System.out.print("date is:"+format);
}
}
This is working fine and output is: date is:2014-02-11
But when I use same syntax in Service layer of spring MVC than it results as:
date is:????-??-??
Here is code in spring. There is an #Override function in which I have used it.
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date = new Date();
String format = sdf.format(date);
logger.info("current date is:"+format);
What could be the error?
#InitBinder
public void initBinder(WebDataBinder webDataBinder) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
dateFormat.setLenient(false);
webDataBinder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
}
I want to convert the selected date into the format"dd-MM-yyyy" because c.getTime returns "Sun Mar 3 12:34:46 IST 2013"
private class MyDateListener implements DateListener {
public void dateChanged(DateEvent e) {
Calendar c = e.getSelectedDate();
if (c != null) {
try {
SimpleDateFormat parser = new SimpleDateFormat(c.getTime().toString());
SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
Date date;
date = parser.parse(date1);
output = formatter.format(date);
System.out.println(output);
} catch (ParseException ex) {
Logger.getLogger(Example1.class.getName()).log(Level.SEVERE, null, ex);
}
} else {
System.out.println("No time selected.");
}
}
}
Do you want:
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
System.out.println(sdf.format(c.getTime());
The getTime() function actually returns a date, which you can pass to your formatter, so there is no need to parse it.