How to parse gujarati date and formate in predefined formate - android

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

Related

Completely reformat a String? - 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;}

How to parse Date format server format To "MM/dd/YYYY"

I was found many solution but its not working for me.
I am trying to parse date to user selected date format.
I was getting date from server in this format 2016-06-30T00:00:00Z UTC Timezone
Now I want to Parse it in User selected format for e.g MM/dd/yyyy
How Can i do that.? I was found many solution but its not working for me.
To get date into a particular format, first you have to parse the date string into it's given format and there you will get Date object. Second, you can convert the date object into a desired format.
private String getFormattedDate(String strDate) {
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
Date d= (Date)formatter.parse(strDate);
SimpleDateFormat fmtOut = new SimpleDateFormat("MM/dd/yyyy");
return fmtOut.format(date);
}
This function help to convert any type of string date in any formate
public String getFormatedDate(String dateString, String format, String currentFormat) {
SimpleDateFormat sdf = new SimpleDateFormat(currentFormat, Locale.ENGLISH);
try {
Date date = sdf.parse(dateString);
String formated = new SimpleDateFormat(format).format(date);
return formated;
} catch (Exception e) {
e.printStackTrace();
return "";
}
}
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");

How to convert "year-month-day" date format(eg: 2015-05-12-which is a value retrieved from server) to "day-month-year" format in android

How to convert "year-month-day" date format(eg: 2015-05-12-which is a value retrieved from server) to "day-month-year" format in android .
The actual value I get from server is in yr-mnth-day format. But I need to display it as day-mnth-yr in my app.How can I achieve this?
First you'll need to parse the String into a Date. Then you format the date according to your needs, the example below is an edited version of BalusC's answer from this topic: Change date format in a Java string
String oldstring = "2015-05-12";
Date date = new SimpleDateFormat("yyyy-MM-dd").parse(oldstring);
//Use SimpleDateFormat#format() to format a Date into a String in a certain pattern.
String newstring = new SimpleDateFormat("dd-MM-yyyy").format(date);
System.out.println(newstring); // 12-05-2015
If you say this, i suppose you mean in a string, so you can convert string to Date with
String dateServer = "2015-05-12";
//Identify the format which has been used from server
DateFormat format = new SimpleDateFormat("yyyy, MM, dd", Locale.ENGLISH);
//Convert string to Date using this format
Date date = format.parse(string);
//Create the format you need to print
DateFormat wellFormatted = new SimpleDateFormat("dd/MM/yyyy");
//now convert the date into a string formatted as you like
String wellPrintedDate = wellFormatted.format(date);
that's all, "wellPrintedDate" is the string in format day/month/yeah. here you can find the list of code used for formatting :)
try {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Date past = format.parse("2015-05-12");
SimpleDateFormat format1 = new SimpleDateFormat("dd-MM-yyyy");
System.out.println("dt=" + format1.format(past));
} catch (ParseException e) {
e.printStackTrace();
}
public static String formattedDateFromString(String inputFormat, String outputFormat, String inputDate){
if(inputFormat.equals("")){ // if inputFormat = "", set a default input format.
inputFormat = "yyyy-MM-dd";
}
if(outputFormat.equals("")){
outputFormat = "dd-mm-yyyy"; // if inputFormat = "", set a default output format.
}
Date parsed = null;
String outputDate = "";
SimpleDateFormat df_input = new SimpleDateFormat(inputFormat, java.util.Locale.getDefault());
SimpleDateFormat df_output = new SimpleDateFormat(outputFormat, java.util.Locale.getDefault());
// You can set a different Locale, This example set a locale of Country Mexico.
//SimpleDateFormat df_input = new SimpleDateFormat(inputFormat, new Locale("es", "MX"));
//SimpleDateFormat df_output = new SimpleDateFormat(outputFormat, new Locale("es", "MX"));
try {
parsed = df_input.parse(inputDate);
outputDate = df_output.format(parsed);
} catch (Exception e) {
Log.e("formattedDateFromString", "Exception in formateDateFromstring(): " + e.getMessage());
}
return outputDate;
}
call like this
Log.e("",""+formattedDateFromString("yyyy-MM-dd","dd-MM-yyyy","2015-05-12"));
output: 12-05-2015

How to get the Month from date string in sql server

As stated in the Title of the question , i have different days in different accounts, How to get the date part of these
Just change the date formats below depending on your date format:
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
try {
Date date = (Date)formatter.parse(str);
DateFormat df = new SimpleDateFormat("MM"); // you can use 'dd' for date
return df.format(date);
} catch (ParseException e) {
e.printStackTrace();
return null;
}

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