📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.
✅ Some premium posts are free to read — no account needed. Follow me on Medium to stay updated and support my writing.
🎓 Top 10 Udemy Courses (Huge Discount): Explore My Udemy Courses — Learn through real-time, project-based development.
▶️ Subscribe to My YouTube Channel (172K+ subscribers): Java Guides on YouTube
Learn and master in Java 8 features at Java 8 Tutorial with Examples.
Get Milliseconds from LocalDateTime in Java 8
package com.java.tutorials.collections;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
/**
* Convert LocalDateTime to long
* @author Ramesh Fadatare
*
*/
public class LocalDateTimeExample {
public static void main(String[] args) {
LocalDateTime localDateTime = LocalDateTime.now();
ZonedDateTime zdt = ZonedDateTime.of(localDateTime, ZoneId.systemDefault());
long date = zdt.toInstant().toEpochMilli();
System.out.println(date);
}
}
1584458975530
Add Day, Hour and Minute to LocalDateTime
package com.java.tutorials.collections;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
public class LocalDateTimeExample {
public static void main(String[] args) {
LocalDateTime dateTime = LocalDateTime.now();;
System.out.println(dateTime);
// add days
dateTime = dateTime.plusDays(10L);
System.out.println(dateTime);
// add hours
dateTime = dateTime.plusHours(10L);
System.out.println(dateTime);
// add minutes
dateTime.plusMinutes(10L);
System.out.println(dateTime);
ZonedDateTime zonedDateTime = ZonedDateTime.of(dateTime, ZoneId.systemDefault());
long date = zonedDateTime.toInstant().toEpochMilli();
System.out.println(date);
}
}
2020-03-17T21:01:36.501682600
2020-03-27T21:01:36.501682600
2020-03-28T07:01:36.501682600
2020-03-28T07:01:36.501682600
1585359096501
Comments
Post a Comment
Leave Comment