Covert UTC time format to local time [duplicate] - android

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())'

Related

how to reformat date in android studio [duplicate]

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.

How to convert Api String date to mm-dd-yy date format (Android) without the use of datepicker?null Pointer exception

I am getting Time from an api in this format
"2018-03-07T11:19:54.686Z" ..... How to convert this String data to mm-dd-yyyy date format in android.
SimpleDateFormat format = new SimpleDateFormat(" MM,dd,yy");
String date = format.format(Date.parse(data));
I have tried using the above two but there is a parse error.
First you have to parse the given date "2018-03-07T11:19:54.686Z" in the same format and then you can apply formatting with the desired format.
SimpleDateFormat parseDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
String date = format.format(parseDateFormat.parse(data));
you used below code and show your format according to date.
String date="2018-03-07T11:19:54.686Z";
SimpleDateFormat parseDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
DateFormat formatdate = new SimpleDateFormat("MM,dd,yy");//if show mm-dd-yy
try {
Date date1=parseDateFormat.parse(date);
Log.d("New Date",formatdate.format(date1));
} catch (ParseException e) {
e.printStackTrace();
}

Exception on Date parsing android [duplicate]

This question already has answers here:
java.text.ParseException: Unparseable date
(3 answers)
Closed 5 years ago.
I am using a function to take out the difference between a given date and current date. At all the places, that function is working just fine but at one place, it is throwing the unparseable date exception.
Below is the function that I use:
public boolean timeDiff24hrs(String alarmTime) {
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy'T'HH:mm:ss");
sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
Date date = new Date();
String s = sdf.format(date);
String alarmDateStr="";
try {
Date alarmDate=sdf.parse(alarmTime);
alarmDateStr=sdf.format(alarmDate);
} catch (ParseException e) {
e.printStackTrace();
}
int diff = diffOf2Days(alarmDateStr, s);
if (diff > 1439) {
return true;
}
return false;
}
Here I get the exception trying to execute the try block.
The alarmDate that I pass into the function is :
1/31/2018 8:58:12 AM +00:00
Can someone please explain.
Your date format MM/dd/yyyy'T'HH:mm:ss which your using is incorrect 1/31/2018 8:58:12 AM +00:00 and return different value from expected.
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy'T'HH:mm:ss");
return 1/31/2018T8:58:12
And you are trying to pass below
1/31/2018 8:58:12 AM +00:00

can i change the date 1510808400000 to a valid date format in android [duplicate]

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

How to convert a date using the langage of the device? [duplicate]

This question already has answers here:
Date formatting based on user locale on android
(8 answers)
Closed 7 years ago.
In my Android project, I have 3 variables (string) :
String day = "26";
String month = "03";
String year = "1989";
I would like to have a variable (String date) with the date format of the device (depending of the langage) like this :
If the device is in french, date = "26/03/1989"
If the device is in english (USA), date = "03/26/1989"
etc
How can I do that ?
Thanks
Join your strings into one like in this example (obviously you can omit the date)
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
EDIT: This example doesn't show how to get the phone local date format
SimpleDateFormat already support that.
You can especify your desired Locale or get the default from the device.
Take a look at the docs.
Hope this helps.

Categories

Resources