How to Convert Currenttimemillis to a Date in Java

How to convert currentTimeMillis to a date in Java?

Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(timeStamp);

int mYear = calendar.get(Calendar.YEAR);
int mMonth = calendar.get(Calendar.MONTH);
int mDay = calendar.get(Calendar.DAY_OF_MONTH);

Convert millisecond String to Date in Java

double tempo=Double.parseDouble(z);

Why are you parsing your String which is supposed to be a Long as a Double?

Try using Long.parseLong:

String x = "1086073200000"

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

long milliSeconds= Long.parseLong(x);
System.out.println(milliSeconds);

Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(milliSeconds);
System.out.println(formatter.format(calendar.getTime()));

How to transform currentTimeMillis to a readable date format?

It will work.

long yourmilliseconds = System.currentTimeMillis();
SimpleDateFormat sdf = new SimpleDateFormat("MMM dd,yyyy HH:mm");
Date resultdate = new Date(yourmilliseconds);
System.out.println(sdf.format(resultdate));

How Do I Convert System.currentTimeMillis To Time Format? (HH:MM:SS)

There are some object that you can use to make this more easy, like SimpleDateFormat and Date.

First prepare the time in mills:

Long currentTime = System.currentTimeMillis();

Choose your desier format with SimpleDateFormat:

SimpleDateFormat simpleDateFormat = new SimpleDateFormat("hh:mm:ss");

Create your date object:

Date date = new Date(currentTime);

Apply that format into your date object:

String time = simpleDateFormat.format(date);

Log it:

Log.d(TAG, "onCreate: " + time);

Result:

17:05:73

Convert String to Date with Milliseconds

The Date class stores the time as milliseconds, and if you look into your date object you will see that it actually has a time of 1598515567413 milliseconds.

You are fooled by the System.out.println() which uses Date's toString() method. This method is using the "EEE MMM dd HH:mm:ss zzz yyyy" format to display the date and simply omits all milliseconds.

If you use your formatter, which has milliseconds in its format string, you will see that the milliseconds are correct:

System.out.println(formatter.format(dateFormatter));

outputs 2020-08-27T10:06:07.413

how to convert milliseconds to Time of day?

Calendar cStart = Calendar.getInstance();
cStart.setTimeInMillis(start);
Calendar cEnd = Calendar.getInstance();
cEnd.setTimeInMillis(end);
Calendar cTest = Calendar.getInstance();
cTest.setTimeInMillis(testtime);

if(cStart.before(cTest) && cEnd.after(cTest)) {
// testtime is between start and end
}

How can I create a Java 8 LocalDate from a long Epoch time in Milliseconds?

If you have the milliseconds since the Epoch and want to convert them to a local date using the current local timezone, you can use Instant.ofEpochMilli(long epochMilli)

LocalDate date =
Instant.ofEpochMilli(longValue).atZone(ZoneId.systemDefault()).toLocalDate();

but keep in mind that even the system’s default time zone may change, thus the same long value may produce different result in subsequent runs, even on the same machine.

Further, keep in mind that LocalDate, unlike java.util.Date, really represents a date, not a date and time.

Otherwise, you may use a LocalDateTime:

LocalDateTime date =
LocalDateTime.ofInstant(Instant.ofEpochMilli(longValue), ZoneId.systemDefault());


Related Topics



Leave a reply



Submit