59

日付の表示に問題があり、タイムスタンプを 1379487711 として取得していますが、実際の時刻は 2013 年 9 月 18 日 12:31:51 PM ですが、時刻は 17-41-1970 と表示されます。現在時刻として表示する方法。

時間を表示するために、次の方法を使用しました。

private String getDate(long milliSeconds) {
    // Create a DateFormatter object for displaying date in specified
    // format.
    SimpleDateFormat formatter = new SimpleDateFormat("dd-mm-yyyy");
    // Create a calendar object that will convert the date and time value in
    // milliseconds to date.
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis((int) milliSeconds);
    return formatter.format(calendar.getTime());
} 
4

10 に答える 10

10

タイムスタンプを現在の日付に変換します。

private Date getDate(long time) {    
    Calendar cal = Calendar.getInstance();
       TimeZone tz = cal.getTimeZone();//get your local time zone.
       SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy hh:mm a");
       sdf.setTimeZone(tz);//set time zone.
       String localTime = sdf.format(new Date(time) * 1000));
       Date date = new Date();
       try {
            date = sdf.parse(localTime);//get local date
        } catch (ParseException e) {
            e.printStackTrace();
        }
      return date;
    }
于 2013-09-21T07:45:25.250 に答える
0

NEWを使用 - > Java.Time for Android Apps Targeting > API26

DATETIMESTAMP を保存しています

 @RequiresApi(api = Build.VERSION_CODES.O)
    public long insertdata(String ITEM, String INFORMATION, Context cons)
    {
        long result=0; 

            // Create a new map of values, where column names are the keys
            ContentValues values = new ContentValues();
            
            LocalDateTime INTIMESTAMP  = LocalDateTime.now();
            
            values.put("ITEMCODE", ITEM);
            values.put("INFO", INFORMATION);
            values.put("DATETIMESTAMP", String.valueOf(INTIMESTAMP));
        
            try{

                result=db.insertOrThrow(Tablename,null, values);            

            } catch (Exception ex) {
            
                Log.d("Insert Exception", ex.getMessage());
                
            }

            return  result;

    }   

INSERTED DATETIMESTAMP WILL BE IN LOCAL DATETIMEFORMAT [ 2020-07-08T16:29:18.647 ] 表示に適しています。

それが役に立てば幸い!

于 2020-07-07T11:40:11.350 に答える