简体   繁体   中英

DateFormat to hours and minutes from inside a list view

I am kinda find some difficulties on a project I am currently building. To help you understand I have the following screenshot.

加入联赛

I retrieve the league's name and the date via a web service. The date represents when the league starts. Hence, League A starts next day at 11:00:50.

And here is how I retrieve those objects.

try {
                        if (response.getString("status").equals("success")) {
                            //getTeam(username, password);

                      JSONArray jsonArray = response.getJSONArray("leagues");

                      for (int i = 0; i < jsonArray.length(); i++) {
                                showOpenLeagues = new   ShowOpenLeagues();

 JSONObject jsonObject1 = jsonArray.getJSONObject(i);
                                showOpenLeagues.setLeagueName(jsonObject1.getString("league_name"));
                                showOpenLeagues.setLeagueStart(jsonObject1.getString("league_start"));
                                showOpenLeagues.setLeagueId(jsonObject1.getString("ID"));
                                showOpenLeaguesList.add(showOpenLeagues);



                                listView.setAdapter(openLeaguesAdapter);


                            }

                        }

                    } catch (JSONException e) {
                        Log.e("TAG", e.toString());
                    }

Nothing more than parsing a json array.

Now here is my problem. I want to find a way of getting all those date formats and convert them to hours and minutes WITHOUT CLICKING IN ANY ROW!!!. I did something similar,but I displayed the timer(using the CountDownTimer class and its overridden methods) in a single textview which is much easier.

Thank you.

You can format the date using SimpleDateFormat .

As you are getting date in format

yyyy-MM-dd HH:mm:ss

and you want it to format

HH:mm

do it like :

SimpleDateFormat df_input = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", java.util.Locale.getDefault());
SimpleDateFormat df_output = new SimpleDateFormat("HH:mm", java.util.Locale.getDefault());

try {
  Date parsed = df_input.parse(inputDate);
  String  outputDate = df_output.format(parsed);
} catch (Exception e) { 
}

now you have a string => outputDate that will only contain Hours and minutes. There can be many combinations possible, ie

"yyyy-MM-dd",
"yyyy-MM-dd HH:mm",
"yyyy-MM-dd HH:mmZ",
"yyyy-MM-dd HH:mm:ss.SSSZ",
"yyyy-MM-dd'T'HH:mm:ss.SSSZ",

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM