I have in String variable this ... "2015-01-12 19:00:00" So I want to convert to DateTime value , I have tried with this....
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/mm/yyyy HH:mm:ss");
Date d = dateFormat.parse(fecha);
But I can't compile, I have gotten this message ... cannot convert from java.util.Date to java.sql.Date
How fix it?
The pattern is actually wrong, try this
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date d = dateFormat.parse(fecha);
Check if you have imported java.sql.Date instead of java.util.Date.
Related
I want today's date in my application to store it in a database as a string but when i used simpledatefromat and calender.getInstance(), it's not giving me proper date. The code is as below.
I tried simpledateformat's another constructor which takes locale as parameter but still not showing proper date
Date date = Calendar.getInstance().getTime();
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-mm-yyyy");
String dateF = dateFormat.format(date);
expected date should be 21-04-2019
actual result 21-26-2019
To get the month like the way that you want, you need to change "dd-mm-yyyy" to "dd-MM-yyyy"
Date date = Calendar.getInstance().getTime();
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy", Locale.US);
String dateF = dateFormat.format(date);
I would like to convert this time stamps to Long value.
2016-07-13T21:11:45+00:00
I still don't know what the format of above time stamp, like MM/dd/yyyy hh:mm:ss aa to use with the SimpleDateFormat.
Try this code to
String timeStr = "2016-11-01T09:45:00.000+02:00";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
Date dateObj= sdf.parse(timeStr);
System.out.println(dateObj.getTime());
It would be yyyy-MM-dd\'T\'HH:mm:ss and you can convert it to long by:
public static long convertDateToMilliseconds(String fromFormat, String sourceDate) {
SimpleDateFormat sdfFrom = new SimpleDateFormat(fromFormat);
//SimpleDateFormat sdfTo = new SimpleDateFormat(toFormat);
Date date = sdfFrom.parse(sourceDate);
//String convertedDate = sdfTo.format(date);
return date.getTime();
}
Use this link to find in which formate to create Simple date formater then create date object with that date formater. Then you can get time in millis like below
long millisecond = beginupd.getTime();
SimpleDateFormat almost states this in its examples. For that you can use
"yyyy-MM-dd'T'HH:mm:ssXXX"
I know how SimpleDateFormat works, by grabbing sdfDateTime.format(new Date(System.currentTimeMillis())); which is today's date from the System. Can SimpleDateFormat format a date by grabbing a string? Let's say you had a string: String dateStr = "04/05/2012"; how would you format that into: "April 5, 2012"?
DateFormat inputFormat = new SimpleDateFormat("MM/dd/yyyy");
inputFormat.setLenient(false);
DateFormat outputFormat = new SimpleDateFormat("MMMM dd, yyyy");
outputFormat.setLenient(false);
String inputDateAsString = "04/05/2012";
Date inputDate = inputFormat.parse(inputDateAsString);
System.out.println(outputFormat.format(inputDate));
You can't just grab an arbitrary string and figure out what its format is.
check this one, it is very helpful library, easy to use & extend for such needs
https://bitbucket.org/dfa/strtotime/wiki/Home
By using the below code i get 43753-07-31T04:40:44-0500
long installed = 1318562444444;
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
String dateString = formatter.format(new Date(installed * 1000L));
HELP :(
Installed is already in milliseconds.
Try this..
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
I have a string which i retrieve from the database which is of the following format
30-08-2010 12:30:00
i need to convert that string into the following format
Aug 30 2010 12:30 PM
How do i do this?
Also tell me how to convert the string into a date object.
thank you in advance.
look around java.util.* for more info. It has nothing to do with Android.
DateFormat formatter1 = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
Date date = (Date)formatter1.parse("30-08-2010 12:30:00");
DateFormat formatter2 = new SimpleDateFormat("MMM dd yyyy hh:mm a");
String newFormattedString = formatter2.format(date);