This question already has answers here:
Java : Cannot format given Object as a Date
(7 answers)
Closed 2 years ago.
i have an database of order and it order date
then it order date will be uploaded to database like this (3-3-2020)
then in my shop application, i call the database using JSON, and i want to convert 3-3-2020 to Tuesday, March, 2020
i try this solution
pertemuan = getIntent().getStringExtra("pertemuan");
SimpleDateFormat format = new SimpleDateFormat("EEEE, MMMM d, yyyy");
String tanggal = format.format(pertemuan);
pertemuanview.setText(tanggal);
but it give me this error
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.app.appguru/com.app.appguru.Detail_Pesanan}: java.lang.IllegalArgumentException: Cannot format given Object as a Date
then how to reformat the date?
You are passing wrong object in format.format it suppose to be Date object and you are passing String thats why it is throwing IllegalArgumentException.
First you need to convert your string date to date object and pass to formatter like below.
SimpleDateFormat format = new SimpleDateFormat("EEEE, MMMM d, yyyy");
SimpleDateFormat currentDateformat = new SimpleDateFormat("dd-MM-yyyy");
Date date = null;
try {
date = currentDateformat.parse("3-3-2020");
} catch (ParseException e) {
e.printStackTrace();
}
String tanggal = format.format(date);
Hope this will help you.
Related
This question already has answers here:
How to convert Date to a particular format in android?
(7 answers)
Closed 3 years ago.
I have date in below format coming from api response and in pojo i take it as string and now date is taken as string and my concern is Date is deprecated in api 16.
My main question is how to convert this and assign some date format to it.
"date":1561040449000"
historyList.get(position).getTxnDate() = "1561040449000"
long dateToLong = Long.parseLong(historyList.get(position).getTxnDate());
SimpleDateFormat sdf = new SimpleDateFormat("dd MMMM yyyy", Locale.getDefault());
String dateString = sdf.format(new Date(dateToLong));
Pass your long typed "date" timestamp to this method and it will return date in specified format:
public static String getDateFromTimeStamp(long date) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return formatter.format(new Date(date));
}
This question already has answers here:
how to convert long date value to mm/dd/yyyy format [duplicate]
(4 answers)
Closed 5 years ago.
I am working on some error like i am getting response of a date which is like 1510808400000 and i have to display this as a valid date format like dd-mm-yy Suggest me is there any way to convert that long value to valid date format. or guide me possible changes.
modify date format as required.
private String getDate(long timeStamp){
try{
SimpleDateFormat sdf = new SimpleDateFormat("MM dd, yyyy");
Date netDate = (new Date(timeStamp));
return sdf.format(netDate);
}
catch(Exception ex){
ex.printStackTrace();
return null;
}
}
Use below code
long millis = Long.parse("1510808400000 ");
Date date = new Date(millis);
String displayDate = new SimpleDateFormat("dd-mm-yy").format(date);
This question already has answers here:
Convert UTC to current locale time
(7 answers)
Closed 9 years ago.
I have a UTC time in this format 2013-03-04T14:37:15 How can I convert this into local time and then to one minute ago format.
I tried this first to convert the time to local format:
private String getDate(String date) {
Date fDate = null;
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
try {
fDate = simpleDateFormat.parse(date);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return fDate.toString();
}
But the above functions throws an exception saying unparseable date..
The parse exception you get is because of the extra T in your date(2013-03-04T14:37:15).
You can either remove that extra 'T' and parse the date with your current date format,
Or if you need to parse it with the T, change your format to parse the literal T as well,
new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
Check DateUtils.getRelativeTimeSpanString() (one methods with the same name from DateUtils). It will be something like :
Date fDate = null;
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
fDate = simpleDateFormat.parse(date);
return DateUtils.getRelativeTimeSpanString(context, System.currentTimeMillis()-fDate.getTime())'
Working on Contact birthday and Anniversary:
I get details and birthday like this 12.2.2012 or 12/2/2012 or 12-02-2012 or 2/12/12
Question:
Is the date Format same across all Samsung Phones. IF yes what is the date format.
(Guess won't work on all Android phones as birthday dates are stored in many different format)
How to identify the date format like if the date is 12.2.2012 or Feb 12 2012 or any other date string pattern. Is it of format "yyyy-MM-dd" or "MMM dd, yyyy" or any other?
ex: if date is "Feb 12 2012" then date format is "MMM dd yyyy"
Unfortunately it seems different apps/vendors use different formats. My solution for this is to try parsing different formats until i either find the right one or give up.
Here is some sample code:
public static final SimpleDateFormat[] birthdayFormats = {
new SimpleDateFormat("yyyy-MM-dd"),
new SimpleDateFormat("yyyyMMdd"),
new SimpleDateFormat("yyyy.MM.dd"),
new SimpleDateFormat("yy-MM-dd"),,
new SimpleDateFormat("yyMMdd"),
new SimpleDateFormat("yy.MM.dd")
new SimpleDateFormat("yy/MM/dd"),
new SimpleDateFormat("MM-dd"),
new SimpleDateFormat("MMdd"),
new SimpleDateFormat("MM/dd"),
new SimpleDateFormat("MM.dd"),
};
.....
Date birthday = null;
for (SimpleDateFormat f : birthdayFormats) {
try {
birthday = f.parse(birthdaystr);
if (birthday!=null) {
contact.setBirthday(birthday);
break;
}
} catch (Exception e) {
continue;
}
}
Use DateFormat.getDateInstance(int style, Locale locale) instead of creating your own patterns with SimpleDateFormat.
Another way that u want to get date in String and after pass in below code
String dateStr = "04/05/2010";
SimpleDateFormat curFormater = new SimpleDateFormat("dd/MM/yyyy");
Date dateObj = curFormater.parse(dateStr);
SimpleDateFormat postFormater = new SimpleDateFormat("MMMM dd, yyyy");
String newDateStr = postFormater.format(dateObj);
The date is stored as "YYYY-MM-DD".
Use the date formater class to convert it into any format you need.
When you want to update put it in the same format as you have read.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
DateFormat conversion problem in java?
I am trying to convert string to date, but getting error.
I am getting date using :
URL xmlUrl = new URL(path);
URLConnection urlconn = xmlUrl.openConnection();
Date = new Date(urlconn.getLastModified());
and then I ma saving this date in a file , which saves in the following format :
Mon Jun 21 16:31:24 Asia/Karachi 2010
and then when later I read this date from file as a String, I again want to save it to a Date, but I am getting error.
I tried :
DateFormat format = DateFormat.getDateInstance();
date = format.parse(fileDate);
but I am getting error :
java.text.ParseException: Unparseable date: Mon Jun 21 16:31:24 Asia/Karachi 2010
Is there any way i can retrieve back the date.
Thanks
public String getconvertdate1(String date)
{
DateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
inputFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
DateFormat outputFormat = new SimpleDateFormat("dd MMM yyyy");
Date parsed = new Date();
try
{
parsed = inputFormat.parse(date);
}
catch (ParseException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
String outputText = outputFormat.format(parsed);
return outputText;
}
Try this. Have to specify the correct date format.
SimpleDateFormat format = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
Date d = format.parse(fileDate);