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;
}
Related
I have a date String (format: dd-mmm-yyyy) and I would like to convert it into milliseconds, but it has to have the time zone of the device.
I've already tried this, but unfortunetly the "f.parse("17-July-2018");" is not working right.
SimpleDateFormat f = new SimpleDateFormat("dd-MMM-yyyy", Locale.ENGLISH); //Sample "17-July-2018"
try {
Date d = f.parse("17-July-2018");
bonusTime = d.getTime();
} catch (ParseException e) {
e.printStackTrace();
}
The parsing format for full month names is MMMM
SimpleDateFormat f = new SimpleDateFormat("dd-MMMM-yyyy", Locale.ENGLISH);
should work. MMM is for short names like "Jan" or "Feb".
In my android application, I'm parsing data from mysql database using JSON and displaying in android listview. In mysql database, the date format is YYYY-MM-DD and I am getting this format in android using JSON as a String. Is it possible to change date format to DD-MM-YYYY in android instead of database..??
Use SimpleDateFormat to convert date form one format to another format :
SimpleDateFormat df1 = new SimpleDateFormat("YYYY-MM-DD");
SimpleDateFormat df2 = new SimpleDateFormat("DD-MM-YYYY");
try{
Date d = df1.parse(DateString);
System.out.println("new date format " + df2.format(d));
}catch(Exception e){
e.printStackTrace();
}
Yes, it is. Use SimpleDateFormat :
String inputPattern = "yyyy-MM-dd";
String outputPattern = "dd-MM-yyyy";
SimpleDateFormat inputFormat = new SimpleDateFormat(inputPattern);
SimpleDateFormat outputFormat = new SimpleDateFormat(outputPattern);
Date date = null;
String result = null;
try {
date = inputFormat.parse(time);
result = outputFormat.format(date);
} catch (ParseException e) {
e.printStackTrace();
}
//use the result variable as you wish
this will be the answer i need :D
String well=String.valueOf(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",
java.util.Locale.getDefault()).format(new java.util.Date()));
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 am trying to format the date string to Date, and then get the month/day from it:
String strDate="2013-05-15T10:00:00-07:00";
SimpleDateFormat dateFormat = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss-z");
Date convertedDate = new Date();
try {
convertedDate = dateFormat.parse(strDate);
} catch (ParseException e) {
e.printStackTrace();
}
SimpleDateFormat sdfmonth = new SimpleDateFormat("MM/dd");
String monthday= sdfmonth.format(convertedDate);
but it returns me current month/day i.e 5/18. Whats wrong?
3 things :
There is a mistake in your format : 2013-05-15T10:00:00-07:00 has no meaning, it should be 2013-05-15T10:00:00-0700 (with no colon at the end, this is a timezone defined in RFC 822. (Look at the docs about Z).
Change your format to yyyy-MM-dd'T'HH:mm:ssZ as mentionned by #blackbelt
you get a bad date because you re-format your date whatever happens during parsing. Re-format in your try block, if and only if parsing worked.
----------Update
String strDate = "2013-05-15T10:00:00-0700";
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
Date convertedDate = new Date();
try {
convertedDate = dateFormat.parse(strDate);
SimpleDateFormat sdfmonth = new SimpleDateFormat("MM/dd");
String monthday = sdfmonth.format(convertedDate);
} catch (ParseException e) {
e.printStackTrace();
}
I don't know what is wrong in your code. for me it throws Unparseable exception like this.
java.text.ParseException: Unparseable date: "2013-05-15T10:00:00-07:00"
But the following way works well.
String strDate="January 2, 2010";
SimpleDateFormat dateFormat = new SimpleDateFormat("MMMM d, yyyy");
Date date = dateFormat.parse(strDate);
System.out.println(date.getMonth());
But in java Date is deprecated as per http://docs.oracle.com/ . Try to use Calender instead of Date.
I hope this will help you.
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();
}