I think I've got a little Problem by getting a Date from a JSON, put it into an Attribute of a Class and get it from it to show it in a string.
The important party of my Class are:
Date _begin;
public Date getBegin(){ return this._begin; }
public void setBegin(Date begin){ this._begin = begin; }
and I use it with an Testoutput here (the important Part of the JSON loop):
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
dateFormat.setTimeZone(TimeZone.getTimeZone("Europe/Berlin"));
String tempDateString = c.getString("begin");
try {
Date date = null;
try {
date = dateFormat.parse(tempDateString);
} catch (java.text.ParseException e) {
e.printStackTrace();
}
thisData.setBegin(date);
Log.d("SimpleDateFormat(EventActivity Template)", "dd-MM-yyyy HH:mm:ss");
Log.d("SimpleDateFormat(EventActivity String)", tempDateString);
Log.d("SimpleDateFormat(EventActivity Output)", dateFormat.format(thisData.getBegin()));
} catch (ParseException e) {
e.printStackTrace();
}
And the testoutput is:
05-28 10:02:31.588: D/SimpleDateFormat(EventActivity Template)(10396): dd-MM-yyyy HH:mm:ss
05-28 10:02:31.588: D/SimpleDateFormat(EventActivity String)(10396): 2013-09-07 20:00:00
05-28 10:02:31.588: D/SimpleDateFormat(EventActivity Output)(10396): 05-03-0013 20:00:00
Why did I get back the Date in this strange format?
You have to add 1900 to year then add it with date and then the currect value and format of date returns.
Related
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;
}
}
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;}
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;
}
I am having a string that returns both date and time from database as 2012-11-09 13:20:17. Now I need to take the time from the database string and show the time like 13:20:17 pm if the phone time settings is 24 hour and if the phone time settings is 12 hour I should show it as 01:20:17 pm. The time format can be obtained by this line of code which shows whether its 24hr or 12hr
String timefrmt = Settings.System.getString(context.getContentResolver(),Settings.System.TIME_12_24);
SimpleDateFormat timesettingsFormat=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
date="2012-11-09 13:20:17";
Date myDate=null;
try {
myDate=timesettingsFormat.parse(date);
} catch (ParseException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
if(timefrmt==null){
timefrmt="hh:mm aa";
}
SimpleDateFormat timeFormat=new SimpleDateFormat(timefrmt);
String time=timeFormat.format(myDate);
The sample code which I have tried is above but its crashing. Can anyone help me find a way for this.
Thanks a lot
try this
public static String dateFormateNote(String date)
{
try {
DateFormat df1 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
DateFormat df2 = new SimpleDateFormat("hh:mm:ss");
return df2.format(df1.parse(date));
}
catch (ParseException e) {
return date;
}
}
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();
}