Androidda magistraldan millisekundlarda vaqtni qanday olish mumkin?

Mening ilovam juda sodda, u faqat sana va vaqtni o'z ichiga olgan mag'lubiyatga o'zgaruvchidan iborat bo'lib, u faqatgina vaqtni olib kelib, uni millisekundlarga aylantirishim kerak. .

Bu erda mening MainActivity kodi quyidagicha ko'rinadi:

   try {
        String string = "Mon, 10 Mar 2017 03:26:00 p.m.";
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, Integer.parseInt(string.substring(17, 19)));
        calendar.set(Calendar.MINUTE, Integer.parseInt(string.substring(20, 22)));
        calendar.set(Calendar.AM_PM, string.contains("a.m.") ? 0 : 1);
        Toast.makeText(getApplicationContext(), "cal: " + calendar.getTime()+ " , milli sec: "+calendar.getTimeInMillis(), Toast.LENGTH_LONG).show();
    } catch (Exception e) {
        Toast.makeText(getApplicationContext(), "exe: " + e, Toast.LENGTH_LONG).show();
    }

calendar.getTime ni chop qilsam, har ikki sana va vaqtni olaman. Tue Mar 14 15:26:45 GMT + 05: 30 2017 o'rniga, 15:26:45 qiymatini olish va ushbu qiymatni milliy soniyaga aylantirish kerak.

1
c.getTimeInMillis ()
qo'shib qo'ydi muallif TruongHieu, manba
Biz calendar.getTimeInMillis() dan foydalanganimizda biz butun sonni emas, balki millisekundlarda 15:26:45 ni olamizmi?
qo'shib qo'ydi muallif Karthik, manba
Biz calendar.getTimeInMillis() dan foydalanganimizda biz butun sonni emas, balki millisekundlarda 15:26:45 ni olamizmi?
qo'shib qo'ydi muallif Karthik, manba

6 javoblar

Buni ishlatib ko'ring:

try {
    String string = "Mon, 10 Mar 2017 03:26:00 p.m.";
    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.HOUR, Integer.parseInt(string.substring(17, 19)));
    calendar.set(Calendar.MINUTE, Integer.parseInt(string.substring(20, 22)));
    calendar.set(Calendar.AM_PM, string.contains("a.m.") ? 0 : 1);
    DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
    Toast.makeText(getApplicationContext(), "cal: " + dateFormat.format(calendar.getTime())+ " , milli sec: "+calendar.getTimeInMillis(), Toast.LENGTH_LONG).show();
} catch (Exception e) {
    Toast.makeText(getApplicationContext(), "exe: " + e, Toast.LENGTH_LONG).show();
}
1
qo'shib qo'ydi
HOUR_OF_DAYni HOUR bilan almashtiring
qo'shib qo'ydi muallif shmakova, manba
Sizning kodingizni amalga oshirdim, ammo dateFormat.format (calendar.getTime ()) ni bosganimda u joriy tizim vaqtini belgilaydi.
qo'shib qo'ydi muallif Karthik, manba
Thanx @shmakova. Siz mening kunimni saqlab qoldingiz.
qo'shib qo'ydi muallif Karthik, manba

Millisekundlarni olish uchun osongina calendar.getTimeInMillis() dan foydalanishingiz mumkin.

0
qo'shib qo'ydi
Biz calendar.getTimeInMillis() dan foydalanganimizda, biz millisekundlarda milisaniyadagi barcha narsani emas, balki 15:26:45 ni olamizmi?
qo'shib qo'ydi muallif Karthik, manba

Millisekundlarni olish uchun osongina calendar.getTimeInMillis() dan foydalanishingiz mumkin.

0
qo'shib qo'ydi
Biz calendar.getTimeInMillis() dan foydalanganimizda, biz millisekundlarda milisaniyadagi barcha narsani emas, balki 15:26:45 ni olamizmi?
qo'shib qo'ydi muallif Karthik, manba

Buni ko'ring

   SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
    sdf.setTimeZone(TimeZone.getTimeZone("UTC"));

    String inputString = "00:01:30.500";

    Date date = sdf.parse("1970-01-01 " + inputString);
    System.out.println("in milliseconds: " + date.getTime());  
0
qo'shib qo'ydi

Try below code:

try {
    String string = "Mon, 10 Mar 2017 03:26:00 p.m.";
    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.HOUR_OF_DAY, Integer.parseInt(string.substring(17, 19)));
    calendar.set(Calendar.MINUTE, Integer.parseInt(string.substring(20, 22)));
    calendar.set(Calendar.AM_PM, string.contains("a.m.") ? 0 : 1);
    calendar.getTimeInMillis();
    SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
    String time = sdf.format(new Date(calendar.getTimeInMillis()));
    long timeinmili=0;
    try {
        Date date1 = sdf.parse(time);
        timeinmili = date1.getTime();
    } catch (ParseException e) {
        e.printStackTrace();
    }
    System.out.println("Time in Mili Seconds :"+timeinmili );
} catch (Exception e) {
    e.printStackTrace();
}
0
qo'shib qo'ydi
vaqt qatori joriy tizim vaqtini chop etmoqda.
qo'shib qo'ydi muallif Karthik, manba

Try below code:

try {
    String string = "Mon, 10 Mar 2017 03:26:00 p.m.";
    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.HOUR_OF_DAY, Integer.parseInt(string.substring(17, 19)));
    calendar.set(Calendar.MINUTE, Integer.parseInt(string.substring(20, 22)));
    calendar.set(Calendar.AM_PM, string.contains("a.m.") ? 0 : 1);
    calendar.getTimeInMillis();
    SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
    String time = sdf.format(new Date(calendar.getTimeInMillis()));
    long timeinmili=0;
    try {
        Date date1 = sdf.parse(time);
        timeinmili = date1.getTime();
    } catch (ParseException e) {
        e.printStackTrace();
    }
    System.out.println("Time in Mili Seconds :"+timeinmili );
} catch (Exception e) {
    e.printStackTrace();
}
0
qo'shib qo'ydi
vaqt qatori joriy tizim vaqtini chop etmoqda.
qo'shib qo'ydi muallif Karthik, manba