将时间从 API 转换为人类可读 [重复]

Posted

技术标签:

【中文标题】将时间从 API 转换为人类可读 [重复]【英文标题】:Converting time from API into Human readable from [duplicate] 【发布时间】:2018-09-10 12:16:17 【问题描述】:

当我获取地震时间时,我正在使用地震 API,它不是人类可读的,如下所示。

API 中的时间为长格式,我想在列表视图中将其显示为 00/00/0000

这就是我从 API 获取时间和更多数据的方式:

    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        l=(ListView)findViewById(R.id.list_id);
        queue= Volley.newRequestQueue(this);
        request();
        l.setOnItemClickListener(new AdapterView.OnItemClickListener() 
              @Override
              public void onItemClick(AdapterView<?> parent, View view, int position, long id) 
              ApiClass apiClass=list.get(position);
               Uri uri=Uri.parse(apiClass.getUrl());
               Intent intent=new Intent(Intent.ACTION_VIEW,uri);
               startActivity(intent);
              
          );

    


                   @Override
                   public void onResponse(String response) 

                       try 
                           JSONObject ob1=new JSONObject(response);
                           JSONArray ob2=ob1.getJSONArray("features");
                           for (int i=0;i<ob2.length();i++)
                               JSONObject ob3=ob2.getJSONObject(i);
                               JSONObject ob4=ob3.getJSONObject("properties");

                               String title=ob4.getString("title");
                               Double mag=ob4.getDouble("mag");
                               Long time=ob4.getLong("time");
                               String u=ob4.getString("url");

                               list.add(new ApiClass(title,mag,time,u));

                           
                           CustomAdapter adapter=new CustomAdapter(MainActivity.this,list);
                           l.setAdapter(adapter);
                       
                       catch (JSONException e) 
                           e.printStackTrace();
                       

                       // Display the first 500 characters of the response string.
                   
               , new Response.ErrorListener() 
           @Override
               public void onErrorResponse(VolleyError error) 
               Toast.makeText(MainActivity.this,"Error",Toast.LENGTH_SHORT).show();
           
       );

       queue.add(stringRequest);
     

以下是我的自定义适配器 java 类:

    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) 
        View v=convertView;
           if(convertView==null)
               v= LayoutInflater.from(c).inflate(R.layout.custom_layout,parent,false);
           
           ApiClass ob=(ApiClass) arrayList.get(position);
        CircleImageView image=v.findViewById(R.id.image_id);
        TextView tv1=v.findViewById(R.id.title_id);
        TextView tv2=v.findViewById(R.id.magnitude_id);
        TextView tv3=v.findViewById(R.id.time_id);
        tv1.setText(ob.getTitle().toString());
        tv2.setText(ob.getMag().toString());
        tv3.setText(ob.getTime().toString());
        image.setImageResource(R.drawable.mouse);

        return v;
    

这是我的 Apiclass,它返回值以在列表视图中设置值:

    public ApiClass(String title, Double mag, Long time, String url) 
        this.title = title;
        this.mag = mag;
        this.time = time;
        this.url = url;
    

    public String getTitle() 
        return title;
    

    public Double getMag() 

        return mag;
    

    public Long getTime() 

        return  time;
    

    public String getUrl() 
        return url;
    


【问题讨论】:

时间是这样的:1737394537378 只显示数字 上面的长整数类型时间是我得到的一个例子,它不正确 我得到的时间与上面的长整数格式相同 earthquake.usgs.gov/fdsnws/event/1 【参考方案1】:

将长时间戳转换为日期对象

Date date = new Date(long)

然后使用 java 提供的 SimpleDateFormat API 将时间转换为您想要的格式, 例如获取 2018-Mar-1

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MMM-d")
String date = sdf.format(date) // date object created above

【讨论】:

我应该在哪里使用日期对象 通过SimpleDateFormat对象格式化时间戳【参考方案2】:

tl;博士

Instant.ofEpochMilli( 1_737_394_537_378L  )

2025-01-20T17:35:37.378Z

java.time

现代方法使用 java.time 类。

您显然有自 1970 年 1970 年第一刻(UTC,1970-01-01T00:00Z)的纪元以来的毫秒数。

Instant instant =  Instant.ofEpochMilli( 1_737_394_537_378L  ) ;

以标准ISO 8601 格式生成字符串。

String output = instant.toString() ;

2025-01-20T17:35:37.378Z

对于其他格式,请使用DateTimeFormatter。在 Stack Overflow 上搜索已经发布的许多示例和讨论。

要查看某个地区人们使用的挂钟时间中的同一时刻,请应用时区。

ZoneId z = ZoneId.of( "Asia/Kolkata" ) ;
ZonedDateTime zdt = instant.atZone( z ) ;

关于java.time

java.time 框架内置于 Java 8 及更高版本中。这些类取代了麻烦的旧 legacy 日期时间类,例如 java.util.DateCalendarSimpleDateFormat

Joda-Time 项目现在位于maintenance mode,建议迁移到java.time 类。

要了解更多信息,请参阅Oracle Tutorial。并在 Stack Overflow 上搜索许多示例和解释。规格为JSR 310。

您可以直接与您的数据库交换 java.time 对象。使用符合JDBC 4.2 或更高版本的JDBC driver。不需要字符串,不需要java.sql.* 类。

从哪里获得 java.time 类?

Java SE 8Java SE 9 及更高版本 内置。 标准 Java API 的一部分,带有捆绑实现。 Java 9 添加了一些小功能和修复。 Java SE 6Java SE 7 大部分 java.time 功能在ThreeTen-Backport 中向后移植到 Java 6 和 7。 Android java.time 类的 android 捆绑包实现的更高版本。 对于早期的 Android (ThreeTenABP 项目采用 ThreeTen-Backport(如上所述)。见How to use ThreeTenABP…

【讨论】:

【参考方案3】:

这对我有用:

   private String getDate(long time)
    SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy hh:mm a");
    String dateString = formatter.format(new Date(time));
    String date = ""+dateString;
    return date;

【讨论】:

仅供参考,java.util.Datejava.util.Calendarjava.text.SimpleDateFormat 等麻烦的旧日期时间类现在已被 java.time 类所取代。在ThreeTen-Backport 项目中,许多java.time 功能被反向移植到Java 6 和Java 7。在ThreeTenABP 项目中进一步适用于早期的Android。见How to use ThreeTenABP…

以上是关于将时间从 API 转换为人类可读 [重复]的主要内容,如果未能解决你的问题,请参考以下文章

如何将此特定的 varchar 转换为人类可读的日期格式?

如何将纳秒的纪元时间转换为人类可读的?

如何使用 sed 将纪元转换为人类可读的日期时间

Coldfusion从UNIX转换为人类可读时间,而不转换时区之间的时间

将纪元转换为人类可读的日期在 python 中不起作用

将骆驼大小写转换为人类可读的字符串?