17

我想与时间戳分开获取时间和日期。请在这些方面帮助我。我的时间戳示例是 1378798459。

谢谢

4

14 回答 14

38

//试试下面的

SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
String dateString = formatter.format(new Date(Long.parseLong(YOUR TIMESTAMP VALUE)));
txtDate.setText(dateString);    

//你可以把你需要的格式放在这里:

SimpleDateFormat formatter = new SimpleDateFormat("YOUR REQUIRED FORMAT");

于 2013-09-10T11:04:58.200 回答
20

试试这个和我一起工作

public  String getDateCurrentTimeZone(long timestamp) {
        try{
            Calendar calendar = Calendar.getInstance();
            TimeZone tz = TimeZone.getDefault();
            calendar.setTimeInMillis(timestamp * 1000);
            calendar.add(Calendar.MILLISECOND, tz.getOffset(calendar.getTimeInMillis()));
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date currenTimeZone = (Date) calendar.getTime();
            return sdf.format(currenTimeZone);
        }catch (Exception e) {
        }
        return "";
    }
于 2013-09-10T11:04:35.653 回答
12

改进 Pratik Dasa 给出的答案

SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");

在这里,您可以使用以下语法获取各种格式。您可以通过在语法中删除或添加下面给出的术语来玩弄它。

Date and Time Pattern                Result
-----------------------------        ---------------------------------
"yyyy.MM.dd G 'at' HH:mm:ss z"       2001.07.04 AD at 12:08:56 PDT
"EEE, MMM d, ''yy"                   Wed, Jul 4, '01
"h:mm a"                             12:08 PM
"hh 'o''clock' a, zzzz"              12 o'clock PM, Pacific Daylight Time
"K:mm a, z"                          0:08 PM, PDT
"yyyyy.MMMMM.dd GGG hh:mm aaa"       02001.July.04 AD 12:08 PM
"EEE, d MMM yyyy HH:mm:ss Z"         Wed, 4 Jul 2001 12:08:56 -0700
"yyMMddHHmmssZ"                      010704120856-0700
"yyyy-MM-dd'T'HH:mm:ss.SSSZ"         2001-07-04T12:08:56.235-0700
"yyyy-MM-dd'T'HH:mm:ss.SSSXXX"       2001-07-04T12:08:56.235-07:00
"YYYY-'W'ww-u"                       2001-W27-3
于 2019-04-12T06:48:05.023 回答
7

你也可以使用这个:

Calendar c = new GregorianCalendar();  // This creates a Calendar instance with the current time
mHour = c.get(Calendar.HOUR);
mMinute = c.get(Calendar.MINUTE);

还有这个:

获取带有时间戳的当前日期作为字符串 ---

String date = (DateFormat.format("dd-MM-yyyy hh:mm:ss", new java.util.Date()).toString());

只获得时间——</p>

String mytime = java.text.DateFormat.getTimeInstance().format(Calendar.getInstance().getTime());

只获取日期 -

String mydate =java.text.DateFormat.getDateTimeInstance().format(Calendar.getInstance().getTime());
于 2013-09-10T11:09:16.517 回答
7
String time = DateUtils.formatDateTime(this, 1378798459, DateUtils.FORMAT_SHOW_TIME);
String date =  DateUtils.formatDateTime(this, 1378798459, DateUtils.FORMAT_SHOW_DATE);
于 2016-02-24T19:19:14.170 回答
4

尝试这个,

final Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(System.currentTimeMillis());
Date date = cal.getTime();
mHour = date.getHours();
mMinute = date.getMinutes();
于 2013-09-10T11:01:43.453 回答
2

只有这样:

long timestampString =  Long.parseLong("yourString");
String value = new java.text.SimpleDateFormat("dd/MM/yyyy HH:mm:ss").
    format(new java.util.Date(timestampString * 1000));
于 2015-11-30T15:30:48.327 回答
1

从时间戳中获取日期和日期的最佳方法是:

java.util.Date dayAndDate = new java.util.Date( (long) yourTimeStamp * 1000);

// 对象如下:Tue Feb 09

String day = dayAndDate.toString().split(" ")[0];

String month = dayAndDate.toString().split(" ")[1];

String date = dayAndDate.toString().split(" ")[2];

我希望你会喜欢我的方法,如果你喜欢它,不要忘记给它一个upvote,以便其他人会考虑它。

于 2021-02-08T23:37:33.067 回答
0
long dv = Long.valueOf(timestamp_in_string)*1000;// its need to be in milisecond
Date df = new java.util.Date(dv);
String vv = new SimpleDateFormat("MM dd, yyyy hh:mma").format(df);

从这里。

于 2013-09-10T11:01:17.617 回答
0

你可以用这个

    Long tsLong = System.currentTimeMillis();
    String ts = tsLong.toString();

    long millisecond = Long.parseLong(ts);
    datetimeString = DateFormat.format("MM-dd-yyyy hh:mm:ss a", new Date(millisecond)).toString();
    timeString = datetimeString.substring(11);
    dateString = datetimeString.substring(0,10);
    String t2 = datetimeString.substring(20,21);

datetimeString 包含日期时间 AM/PM 数据 timeString 将为您提供仅包含时间的子字符串,而 dateString 是日期的子字符串 String t2 将为您提供时钟中的 AM 或 PM

于 2017-06-02T20:34:59.843 回答
0
int day, month, year;
                int second, minute, hour;
                GregorianCalendar date = new GregorianCalendar();

                day = date.get(Calendar.DAY_OF_MONTH);
                month = date.get(Calendar.MONTH);
                year = date.get(Calendar.YEAR);

                second = date.get(Calendar.SECOND);
                minute = date.get(Calendar.MINUTE);
                hour = date.get(Calendar.HOUR);

               String   data  =(hour+ ':'+  ""+minute+ ':'+"" +second+"" +""+"" +day+"" +"/" +(month+1)+"" +"/"+ +year);



Toast.makeText(getActivity(), "Time stamp:"+data,Toast.LENGTH_LONG).show();
于 2016-08-17T10:29:33.183 回答
0

所有的答案都很好,他们主要专注于将其转换unix timestampmilliseconds第一个,这是正确的。

我很难应用它,因为我必须1000L在转换中使用(而1000不仅仅是)。这是我的时区转换工作代码

// Set TimeZone
SimpleDateFormat dateFormat = new SimpleDateFormat("MM-dd-yy h:mm a", Locale.US);
dateFormat.setTimeZone(getDeviceTimeZone());
// Set time
Date date = new Date(timestamp * 1000L);
return dateFormat.format(date);

对于 Android API 26 及更高版本,您可以这样做

return Instant.ofEpochSecond( timestamp )
        .atZone(ZoneId.of( timezone ))
        .toLocalDateTime()
        .toString();
于 2020-10-16T02:35:33.070 回答
0
DateFormat dateFormat = DateFormat.getDateTimeInstance();

when.setText(dateFormat.format(new Date(timestamp * 1000)));

时间戳乘以 1000 以将秒转换为毫秒。

于 2019-06-04T07:47:35.670 回答
0

If you want to use time like in a WhatsApp message, You can use this method,

public static String millisToDateChat(long time) {

        long currentTime = System.currentTimeMillis();
        long defe = currentTime - time;
        long  time_in;

        if(time!=0){   
            time_in = time;   
        }else{
            time_in = currentTime;
            defe = 0;
        }

        int s = (int)defe/1000;
        int m = (int)defe/(1000*60);
        int h = (int)defe/(1000*60*60);
        int d = (int)defe/(1000*60*60*24);
        int w = (int)defe/(1000*60*60*24*7);

        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(time_in);
        Date date = calendar.getTime();
        @SuppressLint("SimpleDateFormat") String formattedDate=(new SimpleDateFormat("HH:mm")).format(date);
        @SuppressLint("SimpleDateFormat") String formattedYear=(new SimpleDateFormat("MMM d, ''yy")).format(date);
        @SuppressLint("SimpleDateFormat") String formattedm=(new SimpleDateFormat("MMM d")).format(date);

        if(d>365) {
            return formattedYear;
        }else if(s>172000){
            return formattedm;
        }else if(s>86400) {
            return "Yest.";
        }else{
            return formattedDate;
        }
    }
于 2021-02-28T20:59:37.667 回答