Completely reformat a String? - Android - android

Currently i am pulling data from an API and loading it into my App. It comes in JSON and a particular string is very ugly to look at. I'm looking to be able to rearrange/reformat this String to make it more pleasing. It's a date/time String.
I've had a look around and have found a few different possibilities, but it's not efficient at all.
Example of what i get, and what i'm after...
JSON: 2016-06-23T02:55:14.907
Formatted: 23/06/2016 - 02:55
Anything along those lines, or just in general. I'm just looking for suggestions of ways i might be able to do it. I'm still fairly new to Android. Anything you can give me would be greatly appreciated.
EDIT 28/06/2016
Here is what i'm doing
dateJSON is: 2016-06-01T21:00:00
public String convertDate(String dateJSON) {
String convertedDate = "";
Format formatter;
Date date = new Date(dateJSON);
formatter = new SimpleDateFormat("dd/MM/yyyy - HH:mm");
convertedDate = formatter.format(date);
return convertedDate;
}
Edit - Solved
public String convertDate(String dateJSON) {
String holder = dateJSON;
try {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
SimpleDateFormat format2 = new SimpleDateFormat("dd/MM/yyyy - HH:mm");
convertedDate = format2.format(format.parse(holder));
return convertedDate;
} catch (ParseException e) {
e.printStackTrace();
}
return convertedDate;
}
Better explanation below

Answer below:
I have passed the string containing the date from JSON into this method.
If you change 'format' to match the date you're passing in (in this case it matches my dateJSON variable), and then set 'format2' to the format you're after on output :)
public String convertDate(String dateJSON) {
String holder = dateJSON;
try {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
SimpleDateFormat format2 = new SimpleDateFormat("dd/MM/yyyy - HH:mm");
convertedDate = format2.format(format.parse(holder));
return convertedDate;
} catch (ParseException e) {
e.printStackTrace();
}
return convertedDate;}

Related

How to parse "2022-02-04T12:20:47,398000+01" into SimpleDateFormat?

My date string looks like this "2022-02-04T12:20:47,398000+01".
I tried this format SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss,SSSSSSZ") but it throws an exception.
Minimum API level that I need to support is 23, so I can't use 'X' symbols in the format like "yyyy-MM-dd'T'HH:mm:ss,SSSSSSX".
Is there a safe way to parse the date string I have on android?
use the following
String inputDate = "2022-02-04T12:20:47,398000+01";
String result;
SimpleDateFormat inputformat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
SimpleDateFormat outputformat = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
try {
Date convertDate = inputformat.parse(inputDate);
result = outputformat.format(convertDate);
tvResult.setText(result);
}catch (Exception e){
e.printStackTrace();
}

How to paste date on desire format Android?

What I am trying to do is get the correct date format in a String.
I got it like this: 2020-05-13T22:00:52+0000
The desire one would be May-13-2020
I tried this:
Date date = new Date();
String insuranceRequestDate = formatDate.format(date);
SimpleDateFormat form = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS",Locale.getDefault());
//java.util.Date date1 = null;
try
{
date = form.parse(insuranceRequestDate);
}
catch (ParseException | java.text.ParseException e)
{
e.printStackTrace();
}
SimpleDateFormat postFormater = new SimpleDateFormat("MMMMM dd, yyyy",Locale.getDefault());
String newDateStr = postFormater.format(date);
But I get an error:
java.text.ParseException: Unparseable date: "2020-05-13T22:00:52+0000" (at offset 19).
Any help or suggestions would be great. By the way, I can´t understand the answers to other questions in case you wonder if there are similar questions, thanks!
You can try this format:
"yyyy-MM-dd'T'HH:mm:ssXX"
instead of
"yyyy-MM-dd'T'HH:mm:ss.SSS"
yyyy-mm-dd'T'HH:MM:SSXX => 2020-04-26T22:12:38+0200
yyyy-mm-dd'T'HH:MM:SS.SSS => 2020-04-26T22:12:38.226
So you are getting ParseException
I solved it actually with a much simpler solution:
Date date = new Date();
String insuranceRequestDate = DateFormat.getDateInstance().format(date);

How to parse gujarati date and formate in predefined formate

I want to parse date like "21 એપ્રિલ, 2121" and want to final output like "21-04-2121".
but i get error in parsing gujarati date.
please help me if anyone do like this.
thank you
Use following function to parse date. And pass inputDateFormat according to your input:
public static String parseStringDate(String dateToConvert, String inputDateFormat) {
SimpleDateFormat inputFormat = new SimpleDateFormat(inputDateFormat, new Locale("guj"));
SimpleDateFormat outputFormat = new SimpleDateFormat("dd-mm-yyyy", Locale.getDefault());
try {
Date date = inputFormat.parse(dateToConvert);
return outputFormat.format(date);
} catch (ParseException e) {
// e.printStackTrace();
return dateToConvert;
}
}
Remember here input date's local (dateToConvert's locale) will be Gujarati.
You can move locale to function parameter and for inputDateFormat you must have to pass. Even if it from server side it is predefined, you can ask to server person.
For any other language you can change the language code in Locale parameter:
public static String parseStringDate(String dateToConvert, String inputDateFormat, Locale locale) {
SimpleDateFormat inputFormat = new SimpleDateFormat(inputDateFormat, locale); // new Locale("guj")
SimpleDateFormat outputFormat = new SimpleDateFormat("dd-mm-yyyy", Locale.getDefault());
try {
Date date = inputFormat.parse(dateToConvert);
return outputFormat.format(date);
} catch (ParseException e) {
// e.printStackTrace();
return dateToConvert;
}
}

Unparseable date: "2017-01-02T01:41:24Z" (at offset 10)

2017-01-02T01:41:24Z is my actual date format and I want to convert this date in yyyy-MM-dd hh:mma format.
Please see the following code I tried so far,
String newsDate = "2017-01-02T01:41:24Z";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mma");
Date date = null;
try
{
date = sdf.parse(newsDate);
}
catch (ParseException e)
{
e.printStackTrace();
}
but at date = sdf.parse(newsDate); line I'm getting the following error:
"Unparseable date: "2017-01-02T01:41:24Z" (at offset 10)".
Please guide me, where could i have gone wrong?
Because you are using different Date Format which is not correct.
Change this
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mma");
to this
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
Try this:
String newsDate = "2017-01-02T01:41:24Z";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd hh:mma");
try {
texview.setText(sdf1.format(sdf.parse(newsDate)));
} catch (ParseException e) {
e.printStackTrace();
System.out.println("bad pattern");
}
This works perfectly for me.
String dateNew= "2017-01-02T01:41:24Z";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd hh:mma");
try {
texview.setText(sdf1.format(sdf.parse(dateNew)));
} catch (ParseException e) {
e.printStackTrace();
System.out.println("bad pattern");
}
The other answers are wrong with respect to time zone / offset - interpretation. The trailing "Z" denotes UTC+00:00 and must not be interpreted just as literal. The documentation of Android defines the suitable pattern symbol X which can handle this kind of input. If you don't care about this detail then you will not get an exception but wrong data (which is even worse).
So the final solution including the fix for the hour-part looks like:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX");
Note: Use XX instead of XXX if you have offsets without colon.

Unable to change the date format in android

From a web service i am getting a date format as 1994-09-11 in my android app i need to display this as Sep 11
My code is as follows
Date date_temp = null;
String d_temp = null;
DateFormat formatter;
static SimpleDateFormat datesdf = new SimpleDateFormat("yyyy MMM dd");
public String changeDateformat(String date_str)
{
Log.e("date_str ",""+date_str);
try
{
date_temp = (Date)formatter.parse(date_str);
Log.e("date_temp ",""+date_temp);
d_temp = null;
d_temp = datesdf.format(date_temp);
Log.e("d_temp ",""+d_temp);
}
catch (ParseException ex)
{
ex.printStackTrace();
}
catch (java.text.ParseException e)
{
e.printStackTrace();
}
return d_temp;
}
When the above method is called, the first log value is been printed(the value which i get from the web), then my app gets crashed saying that
09-10 13:02:44.920: E/AndroidRuntime(3503): java.lang.NullPointerException
09-10 13:02:44.920: E/AndroidRuntime(3503): at xxxxxxxxxxx(Events.java:206)
Here line number 206 is the first line inside try catch of the above method.
What is wrong with my code, pls suggest me.....
DateFormat formatter; <-- it is never initialized. so you are calling a methode on null.
Maybe you want to use:
static SimpleDateFormat datesdf = new SimpleDateFormat("yyyy MMM dd");
Instead of formatter.
You can use string builder
after getting date from web
try this
//wherever you want to set this you can set as below:
enter code here
EditText aa;
aa.setText( new StringBuilder()
// Month is 0 based so add 1
.append(day).append("/")
.append(month + 1).append("/")
.append(year).append(" "));
The DateFormat is not used like that way. You declare a formatter but never initialize it. To format a date, you need to do it like this,
d_temp = DateFormat.format("MMM dd", date_temp).toString();
You need a SimpleDateFormat to convert your date string to date and another SimpleDateFormat to format your date to a new string. Try this code,
public static Date strToDate(String format, String date) {
SimpleDateFormat sdf = new SimpleDateFormat(format);
try {
return sdf.parse(date);
} catch (ParseException e) {
return null;
}
}
public String changeDateformat(String date_str) {
Date t = strToDate("yyyy-MM-dd", date_str);
return DateFormat.format("MMM dd", t).toString();
}

Categories

Resources