Skip to main content

[Solved] Formatting Date & Time in Android App using Java | Java SimpleDateFormat Live Example

 This is bit tricky.

Those familiar with Java, please ignore. Those not, please continue reading how to fix date format using java. Here challenge is that we have fetched record from mySQL Database. Here date is typically stored in yyyy-mm-dd hh:ii:ss format.

Ex. 2021-01-26 12:22:31

The logical flow is important to understantd.

1. MySQL returns a string as date.

2. We convert this strDate to Date Format.

3. Now we change the date format to desired formatting.

4. Next we convert the date to string again.

5. Finally we return the newly formatted date string.

The code blocks:-


String dateCreated = "2021-01-26 12:22:05";

String formattedDate = null;
try {
formattedDate = getFormattedDate(dateCreated);
} catch (ParseException e) {
e.printStackTrace();
}
Toast.makeText(this, formattedDate,LENGTH_SHORT).show();

/* Now create a function */   
public static String getFormattedDate(String strDate) throws ParseException {
Date date = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").parse(strDate);
DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy hh:mm aa");
return dateFormat.format(date).toString();
}

Comments

Popular posts from this blog

How to enable pinch zoom on a web view loading a url with pdf(Android)?

How to enable pinch zoom on a web view loading a url with pdf(Android)? Ref: https://stackoverflow.com/questions/16630382/how-to-enable-pinch-zoom-on-a-web-view-loading-a-url-with-pdfandroid   WebView mWebView = (WebView) findViewById(R.id.webView);         mWebView.getSettings().setJavaScriptEnabled( true ); WebSettings webSettings = mWebView.getSettings(); webSettings.setBuiltInZoomControls( true ); webSettings.setSupportZoom( true );

[SOLVED] Is Android Studio supported on Mac OS 10.15 (Catalina)? Android Studio Not Working on MAC

 Technically Yes! But not that all versions work the same way. While I upgraded to Android Studio to Version 4.1.2. But it did not work in OS 10.15. I deleted my previous android installations following this guide , but still nothing seemed to work. The hell bent installer seemed to get stuck in the middle. I contacted few experts who also appear clueless. A lot of time in my developers' life, I prompt to self - "Read Sid, Read!'. This was no different this time. Instead of searching for help online / offline, I started reading the Android Studio Release Note for version 4.1.2 . It clearly says ,  System Requirements  For MAC:  Mac® OS X® 10.10 (Yosemite) or higher, up to 10.14 (macOS Mojave) So, it may not work with MAC OS Catalina anyway. And it did not work as in my case. Strangely, my previous Android Version 4.0 worked. It is bit tricky to find old releases. But here is where you can find old Android Studio Archives . I downloaded Android Version 4.0, and it wor...

Building Query String in Android

This is pretty interesting. Simply do this. Press Alt + enter to remove the Red lined error messages. It works. It helps to send get Params as Query String. Uri.Builder builder = new Uri.Builder(); builder.scheme( "http" ) .authority( "kmdahousing.in" ) .appendPath( "ApiController" ) .appendPath( "complaints" ) .appendQueryParameter( "UID" , String. valueOf (userID)); String URL = builder.build().toString();