How to change json string of date and format it to only show the day without, year, month, or time

Nancy

I have a JSONObject and an ArrayList from where i get the data, the data date string i get is yyyy/MM/dd 00:00:00, i only want to show the day of the month 1-31, how would i format that string to only show the day ?

I've tried to use SimpleDateFormat, but without any luck, maybe since I'm doing this inside a for loop ?

public void onResponse(String response) {

                        try {
                            //getting the whole json object from the response
                            JSONObject obj = new JSONObject(response);

                            ArrayList<ListModel> ListModelArrayList = new ArrayList<>();
                            JSONArray dataArray = obj.getJSONArray("events");

                            for (int i = 0; i < dataArray.length(); i++) {

                                ListModel List = new ListModel();
                                JSONObject dataobj = dataArray.getJSONObject(i);

                                List.setId(dataobj.getString("id"));
                                List.setInit_date(dataobj.getString("init_date"));
                                List.setEnd_date(dataobj.getString("end_date"));
                                List.setTitle(dataobj.getString("title"));
                                List.setDescription(dataobj.getString("description"));
                                List.setColor_code(dataobj.getString("color_code"));
                                List.setAll_day(dataobj.getString("all_day"));

                                ListModelArrayList.add(List);
                            }

                            for (int j = 0; j < ListModelArrayList.size(); j++) {

                                textViewDate.setText(textViewDate.getText() +
                                        ListModelArrayList.get(j).getInit_Date() + "\n");


                                textViewEvent.setText(textViewEvent.getText() +
                                        ListModelArrayList.get(j).getTitle() + "\n");

                            }

right now i am getting this format 2019-05-17 00:00:00, i want to display 17 only

Hossam Eldeen Onsy

You can alter SimpleDateFormat as you suggested and instead of putting the date directly in the for loop here :

textViewDate.setText(textViewDate.getText() +
                                    ListModelArrayList.get(j).getInit_Date() + "\n");

You will do the following inside your loop :

String s = ListModelArrayList.get(j).getInit_Date();
    SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.ENGLISH);
    Date date = null;
    try {
        date = fmt.parse(s);
        SimpleDateFormat fmtOut = new SimpleDateFormat("dd", Locale.ENGLISH);

        textViewDate.setText(textViewDate.getText() +
                fmtOut.format(date) + "\n");
    } catch (ParseException e) {
        textViewDate.setText(textViewDate.getText() + "\n");
        e.printStackTrace();
    }

Which allows you to format the date according to the pattern you wish in this line :

 SimpleDateFormat fmtOut = new SimpleDateFormat("dd", Locale.ENGLISH);

references for patterns Oracle Documentation SimpleDateFormat Patterns

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

Convert date to format Day, date month year

Matching value on date (year, month, day) but not time

Convert Date String in Format "Mon Day, Year Time am/pm" to POSIXlt format in R?

How to compare only day, month, and year using timestamp?

How to format date without year using Simple Date Format?

Format string for date invalid when converting to decimal day and month

How to get values of Year,Month and Day from String with help of Regex

How to return month and year only except days from date?

convert date in php day/month name, year

この日付形式の正規表現: Day Date Month Year Time TimeZone

How to get results comparing only the day and month, ignoring the time of a timestamp?

R: date format with just year and month

Excel: How to change the date column to display only Month of the date?

How to change date-time format?

How to set day month and year values for a Date or DataAndTime object in Smalltalk (Pharo / Squeak)

Create string of month year in order by date

Group by Date -Month -Day Hour and Time Query

android string datetime format with month and day

Find the closest month-day to a date, no matter the year

GSP date picker: Add label for each field (day, month, year)

R How to change format of date from MM/DD/YYYY to YYYYMMDD and YY-Month (Abbreviated Month)?

How to subtract full months from end_date without crossing the month boundary if end_date falls on the last day of a month?

How to compare current time only without date to a given time in javascript

How to format the given time string and convert to date/time object

Get date, month and year value from Date string

How to change the date/time format to English from the command line?

Convert GMT date format to only time format

How do i get first date and Last date from month and year |Excel |Used Spinner for Month and Year|

How to find last day of specific month of specific year with strtotime() in php

TOP 一覧

  1. 1

    モーダルダイアログを自動的に閉じる-サーバーコードが完了したら、Googleスプレッドシートのダイアログを閉じます

  2. 2

    セレンのモデルダイアログからテキストを抽出するにはどうすればよいですか?

  3. 3

    CSSのみを使用して三角形のアニメーションを作成する方法

  4. 4

    ドロップダウンリストで選択したアイテムのQComboBoxスタイル

  5. 5

    ZScalerと証明書の問題により、Dockerを使用できません

  6. 6

    PyCharmリモートインタープリターはプロジェクトタブにサイトパッケージのコンテンツを表示しません

  7. 7

    Windows 10でのUSB入力デバイスの挿入/取り外しの検出

  8. 8

    Excel - count multiple words per cell in a range of cells

  9. 9

    PictureBoxで画像のブレンドを無効にする

  10. 10

    Windows 10 Pro 1709を1803、1809、または1903に更新しますか?

  11. 11

    スタート画面にシャットダウンタイルを追加するにはどうすればよいですか?

  12. 12

    Python / SciPyのピーク検出アルゴリズム

  13. 13

    Luaの文字列から特定の特殊文字を削除するにはどうすればよいですか?

  14. 14

    Pythonを使用して、リストからデータを読み取り、特定の値をElasticsearchにインデックス付けするにはどうすればよいですか?

  15. 15

    LinuxでPySide2(Qt for Python)をインストールするQt Designerはどこにありますか?

  16. 16

    goormIDEは、ターミナルがロードするデフォルトプロジェクトを変更します

  17. 17

    QGISとPostGIS(マップポイント(米国の地図上にraduisを使用した緯度と経度)

  18. 18

    MLでのデータ前処理の背後にある直感

  19. 19

    ターミナルから「入力ソースの変更」ショートカットを設定する

  20. 20

    パンダは異なる名前の列に追加します

  21. 21

    同じクラスの異なるバージョンを使用したクラスローディング:java.lang.LinkageError:名前の重複クラス定義を試行しました

ホットタグ

アーカイブ