i want to get the value from EditText(InputType: Date) to integer.
Example: 10.01.2017 ==> 20170110
DateFormat df = new SimpleDateFormat("yyyyMMdd");
int inputDate = Integer.valueOf(df.format(etDate.getText().toString()));
How can i fix my problem?
thx and regards
You can parse date. then form the Date object you can have millies
DateFormat df = new SimpleDateFormat("yyyyMMdd");
try {
Date date=df.parse(textView.getText().toString());
long millies=date.getTime();// Here is milli seconds
} catch (ParseException e) {
e.printStackTrace();
}
Edit
To convert one format to another . you can use .
DateFormat df = new SimpleDateFormat("yyyyMMdd");
DateFormat inputFormate = new SimpleDateFormat("dd.MM.yyyy");
try {
Date date=inputFormate.parse(textView.getText().toString());
String formated_date=df.format(date);
// this will be formated_date
} catch (ParseException e) {
e.printStackTrace();
}
Keep that in mind that use can input anything . So you need first check, in which format user entered date it can be (dd/MM/yyyy) or (MM/dd/yyyy).
To get extra data from date or millies use Calender.
Calendar calendar=Calendar.getInstance();
calendar.setTimeInMillis(millies);
int day=calendar.get(calendar.DAY_OF_MONTH);
String text = editText.getText().toString();
String[] array = text.split("\\.");
StringBuilder stringBuilder = new StringBuilder();
for (int i = array.length - 1; i >= 0; i--) {
stringBuilder.append(array[i]);
}
int date = Integer.parseInt(stringBuilder.toString());
I have this stupid way to help you.If you want want to accept this solution, maybe use Regular to solve it is better.
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;}
I am displaying the date and time in Android with this format:
2013-06-18 12:41:24
How can I change it to the following format?
18-jun-2013 12:41 pm
Here is working code
public String parseDateToddMMyyyy(String time) {
String inputPattern = "yyyy-MM-dd HH:mm:ss";
String outputPattern = "dd-MMM-yyyy h:mm a";
SimpleDateFormat inputFormat = new SimpleDateFormat(inputPattern);
SimpleDateFormat outputFormat = new SimpleDateFormat(outputPattern);
Date date = null;
String str = null;
try {
date = inputFormat.parse(time);
str = outputFormat.format(date);
} catch (ParseException e) {
e.printStackTrace();
}
return str;
}
Documentation: SimpleDateFormat | Android Developers
SimpleDateFormat format = new SimpleDateFormat("dd-MMM-yyyy hh:mm a");
String date = format.format(Date.parse("Your date string"));
public class MainActivity extends AppCompatActivity {
private Date oneWayTripDate;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String date ="2017-05-05 13:58:50 ";
SimpleDateFormat input = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
SimpleDateFormat output = new SimpleDateFormat("MMMM dd,yyyy #hh:mm:ss aa");
try {
oneWayTripDate = input.parse(date); // parse input
} catch (ParseException e) {
e.printStackTrace();
}
Log.e("===============","======currentData======"+output.format(oneWayTripDate);
}
}
Use this code like below:
SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy hh:mm a");
String date = formatter.format(Date.parse("Your date string"));
Hope it will help you.
I modified Rusabh's answer for general purpose formatting of date in my project
public String formatDate(String dateToFormat, String inputFormat, String outputFormat) {
try {
Logger.e("DATE", "Input Date Date is " + dateToFormat);
String convertedDate = new SimpleDateFormat(outputFormat)
.format(new SimpleDateFormat(inputFormat)
.parse(dateToFormat));
Logger.e("DATE", "Output Date is " + convertedDate);
//Update Date
return convertedDate;
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
Usage:
String inputFormat = "yyyy-MM-dd'T'HH:mmz";
String OutPutFormat = "MMMM dd', 'yyyy hh:mma";
String convertedDate = formatDate(asOfDateTime, inputFormat, OutPutFormat);
You have to set like this
Date date=new Date("Your date ");
SimpleDateFormat formatter5=new SimpleDateFormat("required format");
String formats1 = formatter5.format(date);
System.out.println(formats1);
set answer like this to set date format
Date date=new Date("Your date ");
SimpleDateFormat formatter5=new SimpleDateFormat("required format");
use this code below:
Date date=new Date("2013-06-18 12:41:24");
SimpleDateFormat formatter5=new SimpleDateFormat("dd-MM-yyyy hh:mm a");
String formats1 = formatter5.format(date);
System.out.println(formats1);
First Create a Calendar object using your Date object. Then build a String using date, year, month and etc you need. then you can use it.
You can get data using get() method in Calendar class.
We can use this format for convert the date. Pass the date as parameter
public String formatdate(String fdate)
{
String datetime=null;
DateFormat inputFormat = new SimpleDateFormat("dd-MM-yyyy");
SimpleDateFormat d= new SimpleDateFormat("yyyy-MM-dd");
try {
Date convertedDate = inputFormat.parse(fdate);
datetime = d.format(convertedDate);
}catch (ParseException e)
{
}
return datetime;
}
private val dateFormatter: Format = SimpleDateFormat(
android.text.format.DateFormat.getBestDateTimePattern(
Locale.getDefault(),
"dMMyyjjmmss"
),
Locale.getDefault()
)
val dateText = dateFormatter.format(Date(System.currentTimeMillis()))
This is the best choice available on Android which will format time based on device default Locate
Example output depending on Locale:
USA - 02/18/2021, 1:00:00 PM (USA uses 12 format time)
Ukraine - 18.2.2012, 13:00:00 (Ukraine uses 24 format time)
So it does all the hard job automatically for you
P.S. replace yy with yyyy if you need 2021 instead of 21, MM to MMM if you want display month as word instead of number and so on
For example "dMMMyyyyjjmmss" for USA will return:
Feb 18, 2021, 1:43:20 PM
For Russia:
18 февр. 2021 г., 13:44:18
Amazing, isn't?
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.
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;
}