This question already has answers here:
How to find difference between two Joda-Time DateTimes in minutes
(4 answers)
Closed 7 years ago.
I have to implement a java program to make the difference between two hours. The problem is that my "hour pattern" is like this:
MM/dd/yyyy HH:mm:ss.SSS
I have to consider ms, too.
I tried using joda:
String sDateStart = "2015/10/14 22:37:28.648";
String sDateFiish = "2015/10/14 22:39:13.573";
SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss.SSS");
Date dt1 = format.parse(sDateFiish);
Date dt2 = format.parse(sDateStart);
DateTime start = new DateTime(dt1);
DateTime finish = new DateTime(dt2);
System.out.println(Days.daysBetween(start, finish));
System.out.println(Minutes.minutesBetween(start, finish));
System.out.println(Seconds.secondsBetween(start, finish));
Is there any method to consider the ms?
If you are using Java API itself. In Date class we have getTime method which gives time in millisecond
getTime()
Returns the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this Date object.
System.out.println((long)(dt1.getTime() - dt2.getTime()));
If you wanna continue using Joda Time API use Duration. as mentioned by Sotirios Delimanolis
Related
This question already has answers here:
String to Date Conversion mm/dd/yy to YYYY-MM-DD in java [duplicate]
(5 answers)
Converting String to Date using SimpleDateFormat is returning random date [duplicate]
(2 answers)
Closed 4 years ago.
I got some values
"createTime": 1527217399000,
"updateTime": 1527218049000,
"createTime": 1527217399000,
"updateTime": 1527217954000,
But I can not parse them to date format, I'm not sure if there is something wrong with the data format,is there any method to parse them?
SimpleDateFormat format = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" );
Long tempTime = 1527217399000L;
Date createDate=format.parse(tempTime.toString());
System.out.println(createDate.toString());
Exception in thread "main" java.text.ParseException: Unparseable date:
"1527217399000"
at java.text.DateFormat.parse(DateFormat.java:366)
If you want to create a Date instance from a long value, use:
Date createDate=new Date(tempTime);
As the Javadoc mentions:
java.util.Date.Date(long date)
Allocates a Date object and initializes it to represent the specified number of milliseconds since the standard base time known as "the epoch", namely January 1, 1970, 00:00:00 GMT.
SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" ) is meant to parse a String of the specified format into a Date (i.e. if you wanted to parse a String such as "2018-05-28 06:12:00" into a Date instance).
Since it's 2018, you really should be making use of the java.time API introduced in Java 8
long createTime = 1527217399000L;
LocalDateTime ldt = Instant.ofEpochMilli(createTime).atZone(ZoneId.systemDefault()).toLocalDateTime();
String format = ldt.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss", Locale.ENGLISH));
System.out.println(format);
Which prints 2018-05-25 13:03:19 (on PC)
And if you don't have Java 8, there's the ThreeTen backport which makes the API available to earlier versions of Java
The old java.util.Date API is notoriously poor and if you want to make clear decisions about what type of date / time you're using - whether it's in a particular timezone, etc. - you should use the java.time API.
Assuming (you'd need to confirm) that these millisecond timestamps are in UTC timezone, you can use code like this to format it as a date/time in your local system timezone.
Long tempTime = 1527217399000L;
Instant instant = Instant.ofEpochMilli(tempTime);
ZonedDateTime dateTime = instant.atZone(ZoneId.systemDefault());
System.out.println(dateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
This question already has answers here:
Convert timestamp in milliseconds to string formatted time in Java
(10 answers)
Closed 5 years ago.
I Have the following code in my application
System.out.println(rec.getDateTrami().getTime());
I need to convert the following format (I suppose that they are seconds)
43782000
29382000
29382000
To a format YYYY-MM-DD HH24:MI:SS, anyone can help to me?
You can make use of the SimpleDateFormat
Example:
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
Date date = new Date();
date.setTime(rec.getDateTrami().getTime());
System.out.println(format.format(date));
Documentation:
SimpleDateFormat,
DateFormat
Use java.time
Best if you can change getDateTrami() to return an OffsetDateTime or ZonedDateTime from java.time. java.time is the modern Java date and time API. It is also known as JSR-310. The code is the same no matter which of the two mentioned types is returned:
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss");
System.out.println(rec.getDateTrami().format(formatter));
This prints a date and time like
2017-12-14 16:52:20
java.time is generally so much nicer to work with than the outmoded Date class and its friends.
If you cannot change the return type
I assume getDateTrami() returns a java.util.Date. Since the Date class is long outmoded, the first thing to do is to convert it to java.time.Instant. From there you perform your further operations:
Date oldfashionedDateObject = rec.getDateTrami();
ZonedDateTime dateTime = oldfashionedDateObject.toInstant()
.atZone(ZoneId.of("Atlantic/Cape_Verde"));
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss");
System.out.println(dateTime.format(formatter));
The result is similar to the above, of course. I on purpose made explicit in which time zone I want to interpret the point in time. Please substitute your own if it doesn’t happen to be Atlantic/Cape_Verde.
Formatting seconds since the epoch
int seconds = 29_382_000;
ZonedDateTime dateTime = Instant.ofEpochSecond(seconds)
.atZone(ZoneId.of("Atlantic/Cape_Verde"));
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss");
System.out.println(dateTime.format(formatter));
This snippet prints
1970-12-06 23:40:00
A date in December 1970. If this is incorrect, it is because 29 382 000 didn’t denote seconds since the epoch of January 1, 1970 at midnight in UTC, also known as the Unix epoch. This is by far the most common time to measure seconds from. If your seconds are measured from some other fixed point in time, I cannot guess which, and you have got a job to do to find out. Again decide which time zone you want to specify.
You could use SimpledateFormat.
new SimpleDateFormat("YYYY-MM-DD HH24:MI:SS").format(date)
This question already has answers here:
Unix epoch time to Java Date object
(7 answers)
Closed 5 years ago.
How can I convert minutes from Unix timestamp to date and time in java? For example, timestamp 1372339860 correspond to Thu, 27 Jun 2013 13:31:00 GMT.
I want to convert 1372339860 to 2013-06-27 13:31:00 GMT.
Edit: Actually I want it to be according to US timing GMT-4, so it will be 2013-06-27 09:31:00.
You can use SimlpeDateFormat to format your date like this:
long unixSeconds = 1372339860;
// convert seconds to milliseconds
Date date = new java.util.Date(unixSeconds*1000L);
// the format of your date
SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss z");
// give a timezone reference for formatting (see comment at the bottom)
sdf.setTimeZone(java.util.TimeZone.getTimeZone("GMT-4"));
String formattedDate = sdf.format(date);
System.out.println(formattedDate);
The pattern that SimpleDateFormat takes if very flexible, you can check in the javadocs all the variations you can use to produce different formatting based on the patterns you write given a specific Date. http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
Because a Date provides a getTime() method that returns the milliseconds since EPOC, it is required that you give to SimpleDateFormat a timezone to format the date properly acording to your timezone, otherwise it will use the default timezone of the JVM (which if well configured will anyways be right)
Java 8 introduces the Instant.ofEpochSecond utility method for creating an Instant from a Unix timestamp, this can then be converted into a ZonedDateTime and finally formatted, e.g.:
final DateTimeFormatter formatter =
DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
final long unixTime = 1372339860;
final String formattedDtm = Instant.ofEpochSecond(unixTime)
.atZone(ZoneId.of("GMT-4"))
.format(formatter);
System.out.println(formattedDtm); // => '2013-06-27 09:31:00'
I thought this might be useful for people who are using Java 8.
You need to convert it to milliseconds by multiplying the timestamp by 1000:
java.util.Date dateTime=new java.util.Date((long)timeStamp*1000);
This question already has answers here:
Unix epoch time to Java Date object
(7 answers)
Closed 5 years ago.
I have following time stamp in Integer form
1333125342
I can convert it using SQL:
select DATEADD(ss, FlOOR(1333089223/86400)*86400, '1970-01-01 00:00:00') AS Date
How to convert it in java? So that it would return value:
3/30/12 12:18:43 PM
Assuming its the time since 1/1/1970 in seconds. you can try
String dateAsText = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
.format(new Date(1333125342 * 1000L));
if it is milliseconds value represents the number of milliseconds that have passed since January 1, 1970 00:00:00.000 GMT
then simply use
new java.util.Date(millis);
and if you need it in particular format
3/30/12 12:18:43 PM
then use SimpleDateFormat to format the Date to desired formatted String
That timestamp contains the seconds elapsed since 1970-1-1 0:00 UTC.
To convert it to a Java Date instantiate a new Date Object (see Java doc) and invoke setTime() on that. Note, that setTime expects milliseconds instead of seconds, so you would have to multiply your timestamp by 1000.
The toString() method yields something readable.
This question already has answers here:
How can I get the current date and time in UTC or GMT in Java?
(33 answers)
Closed 9 years ago.
What is the function to get the current UTC time. I have tried with System.getCurrentTime but i get the current date and time of the device.
Thanks
System.currentTimeMillis() does give you the number of milliseconds since January 1, 1970 00:00:00 UTC. The reason you see local times might be because you convert a Date instance to a string before using it. You can use DateFormats to convert Dates to Strings in any timezone:
DateFormat df = DateFormat.getTimeInstance();
df.setTimeZone(TimeZone.getTimeZone("gmt"));
String gmtTime = df.format(new Date());
Also see this related question.