Android SimpleDateFormat, how to use it?
I assume you would like to reverse the date format?
SimpleDateFormat can be used for parsing and formatting.
You just need two formats, one that parses the string and the other that returns the desired print out:
SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
Date date = fmt.parse(dateString);
SimpleDateFormat fmtOut = new SimpleDateFormat("dd-MM-yyyy");
return fmtOut.format(date);
Since Java 8:
DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd").withZone(ZoneOffset.UTC);
TemporalAccessor date = fmt.parse(dateString);
Instant time = Instant.from(date);
DateTimeFormatter fmtOut = DateTimeFormatter.ofPattern("dd-MM-yyyy").withZone(ZoneOffset.UTC);
return fmtOut.format(time);
Android : Simple Date Format
D - Day in year
and d - Day in month
u can use below-
DateFormat dateFormat1 = new SimpleDateFormat("dd");
String cDay = dateFormat1.format(new Date());
Day.setText(cDay);
Android how to add new date using SimpleDateFormat
Since you don't want to use the Calendar instance you can use LocalDate
. PS:- LocalDate is supported from Java 8.
LocalDate date = LocalDate.now().plusDays(1);
System.out.println("Adding one day to current date: "+date);
How to format date in android environment
You can use SimpleDateFormat
.
SimpleDateFormat dateFormat = new SimpleDateFormat("MMM dd", Locale.US);
Date date = new Date(cal.getTimeInMillis());
String dateStr = dateFormat.format(date);
Android SimpleDateFormat returning dots in am and pm
Try this code. Hope it works !
private String getTimeStamp() {
DateFormat df = new SimpleDateFormat("hh:mm:ss aaa", Locale.ENGLISH);
return df.format(Calendar.getInstance().getTime());
}
SimpleDateFormat(dd/MM/yy kk:mm) showing time as 24:30 instead of 00:30
You should use the uppercase letter K
SimpleDateFormat("dd/MM/yy KK:mm", Locale.getDefault())
k: Hour in day (1-24)
K: Hour in am/pm (0-11)
Check this site to test it out: http://www.sdfonlinetester.info/
is SimpleDateFormat not working in android 5.1.1?
Is locale set to an appropriate value? You can enforce US locale if that's an option for you, the following code works for me:
SimpleDateFormat formatter = new SimpleDateFormat("MMMM dd yyyy hh:mm", Locale.US);
Date x = formatter.parse("November 19 2016 21:54");
Related Topics
How to Sort Alphabetically While Ignoring Case Sensitive
Android Days Between Two Dates
Passing a JavaScript Object Using Addjavascriptinterface() on Android
Android: How to Disable List Items on List Creation
Issue When Using a Custom Font - "Native Typeface Cannot Be Made"
Can't Get Download Url from Firebase Storge in Android
Why Is Accessing Textview of a Fragment Inside Activity Throwing an Error
Android Recyclerview Scrolling Performance
Android Layout with Listview and Buttons
Which Programming Languages How to Use on Android Dalvik
Deprecated Managedquery() Issue
Creating Hashmap from a JSON String
What Is the Easiest Way to Get the Current Day of the Week in Android
Android HTML.Fromhtml() with Images
How to Start Activity in Adapter
How to Implement "CSS Versioning" (To Solve Cache Issues) Using Jsf 2 H:Outputstylesheet