How to convert this milliseconds into time (hh:mm a)? - java

I have milliseconds like "1325085788" i want to convert it on hh:mm a . i know this but return me time 01:34 PM instead of 08:53 PM.Whats problem ?
My code is ::
String created_on = "1325085788";
String pattern = "hh:mm a";
SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
Date date = new Date(Long.parseLong(created_on));
String yourFormatedDate = dateFormat.format(date);
System.out.println("--------> " + yourFormatedDate);
no timezone issue because it give me current time (in india) in iphone app

1325085788 isn't milliseconds. It's seconds. Your expectations are off by a few orders of magnitude, but it's giving you the correct time for the given milliseconds.
If you have seconds and want to go to a Date, then something like this:
import static java.util.concurrent.TimeUnit.*;
String created_on = "1325085788";
long millis = MILLISECONDS.convert(Long.parseLong(created_on), SECONDS);
Date d = new Date(millis);
After that, your date formatting should do the rest of the work.

Related

Converting a string to date format when the String ends in Z

I have to parse the following String into a more readable date format.
String date = "20190112151605.0Z";
However, I've never encountered the Z before. I know it has to do with the time zone but when I try to use my usual code I get a java.lang.NumberFormatException.
My code is as follows:
String whenChanged = "20190112151605.0Z";
long DIFF_NET_JAVA_FOR_DATE_AND_TIMES = 11644473600000L;
long adDate1 = Long.parseLong(whenChanged);
long adLongDate1 = ( adDate1 / 10000 ) - DIFF_NET_JAVA_FOR_DATE_AND_TIMES;
Date lastLogonDate1 = new Date(adLongDate1);
String format2 = new SimpleDateFormat("MM/dd/yyyy
HH:mma'Z'").format(lastLogonDate1);
Any help would be great. Thanks
This will do the trick. The Z means UTC time zone
DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyyMMddHHmmss.Sz");
ZonedDateTime parsed = ZonedDateTime.parse("20190112151605.0Z", fmt);
System.out.println(parsed); // prints 2019-01-12T15:16:05
See DateTimeFormatter
Is it necessary for you to do anything with these time zones?
If not you could do
if(whenChanged.contains('Z')){
whenChanged = whenChanged.substring(0,date.indexOf('Z'));
}

Milliseconds automatically added to java.util.Date / Joda DateTime

I'm trying to create a java.util.Date object without the milliseconds part. eg: 2018-03-19T15:04:23+00:00. This is the code I have:
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX");
Date d = new Date();
String strFromD = sf.format(d);
Date dFromStr = sf.parse(strFromD);
When I debug this and inspect the variables, I see this:
The String which I get by formatting the date does not have any milliseconds. However, when I create a date back from the String, it has the milliseconds part.
I tried the same using Joda DateTime as well:
DateTimeFormatter dateFormatter = ISODateTimeFormat.dateTimeNoMillis();
DateTime dt = new DateTime();
String strFromDt = dateFormatter.print(dt);
DateTime dtFromStr = dateFormatter.parseDateTime(strFromDt);
System.out.println("DT : "+dt);
System.out.println("String from DT : "+strFromDt);
System.out.println("DT from String : "+dtFromStr);
And this is the output:
DT : 2018-03-22T09:30:22.996-07:00
String from DT : 2018-03-22T09:30:22-07:00
DT from String : 2018-03-22T09:30:22.000-07:00
Again, when I try to get the DateTime from the String, it adds the milliseconds back.
Am I missing something here? Do I need to use 2 different formatters or something?
If your SDK expects a java.util.Date, there's no point talking about a format, because dates don't have a format.
The Date class represents one numerical value: the number of milliseconds since Unix Epoch (Jan 1st 1970, at midnight, in UTC). To make a date without the milliseconds, you could truncate this milliseconds value:
Date d = new Date();
// truncate the number of milliseconds since epoch (eliminate milliseconds precision)
long secs = d.getTime() / 1000;
// create new Date with truncated value
d = new Date(secs * 1000);
In Joda-Time, it's a little bit simpler:
// set milliseconds to zero
DateTime dt = new DateTime().withMillisOfSecond(0);
// convert to java.util.Date
Date date = dt.toDate();
If your SDK expects a String, though, then it makes sense talking about formats. A date can be represented (aka "transformed in text") in many different ways:
2018-03-22T09:30:22-07:00
March 22nd 2018, 9:30:22 AM
22/03/2018 09:30:22.000
and so on...
Objects like java.util.Date and Joda's DateTime don't have a format. They just hold values (usually, numerical values), so if your SDK expects one of those objects, just pass them and don't worry about it.
If the SDK expects a String in a specific format (a text representing a date), then you should transform your date objects to that format.
And if this format doesn't allow milliseconds, so be it:
Date d = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX");
String dateFormattedAsString = sdf.format(d);
With Joda-Time:
DateTime dt = new DateTime();
DateTimeFormatter fmt = ISODateTimeFormat.dateTimeNoMillis();
String dateFormattedAsString = fmt.print(dt);
Those will not change the date's values, but the strings won't have the milliseconds printed.

Can't retrieve correct time and date from netflow records.

I used jflow-0.3[1] to collect and decode netflow records which I receive from a router.
nettrack.net.netflow.Flow.java class contains the code for decoding necessary data from records received. I called getFirst() function of it which returns "The time in Coordinated Universal Time (UTC) seconds when this data collection period began."[2]. Then I converted it into date time format using following code.
long seconds = getFirst();
long millis = seconds * 1000;
Date date = new Date(millis);
SimpleDateFormat sdf = new SimpleDateFormat("EEEE,MMMM d,yyyy h:mm,a", Locale.ENGLISH);
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
String formattedDate = sdf.format(date);
System.out.println(formattedDate);
But when I run this I observe weird results for date like Monday,March 2,1970 4:21,PM, Sunday,April 27,2059 4:17,AM,etc. UTC value is also change from very small values like 300 to very large values like 3154137089 which does not make any sense. What is the problem here? how can I solve this?
https://github.com/aptivate/netgraph/tree/master/jflow-0.3
http://www.cisco.com/en/US/docs/net_mgmt/netflow_collection_engine/6.0/tier_one/user/guide/data.html

Convert long time difference to format HH:mm [Java]

How can I convert the difference of the current time a given time to create a string with the time format: HH:mm ? ex. 18:36
I did the following but, it is not 24Hour format, it will add AM/PM to the end, and it is 3 hours off.
java.util.Date today = new java.util.Date();
java.sql.Timestamp ts1 = new java.sql.Timestamp(today.getTime());
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss");
java.util.Date parsedDate = dateFormat.parse(time);
java.sql.Timestamp ts2 = new java.sql.Timestamp(parsedDate.getTime());
long nowTime = ts1.getTime();
long givenTime = ts2.getTime();
long timeDiff = givenTime - nowTime;
//convert to string
java.util.Date d = new java.util.Date(timeDiff);
result = DateFormat.getTimeInstance(DateFormat.SHORT).format(d);
//Outputs: 6:56 PM for example
Once easy thing you can do is call getTime() for both dates and then subtract them like so:
long timeDiff = today.getTime() - ts1.getTime()
That should give you the difference in miliseconds between the two times. After that you know that one second is 1k miliseconds, 1min i 60s, 1h is 60 minutes and so on.
Take a look at Commons Lang DurationFormatUtils.
Or Joda-Time's PeriodFormatter.

How to convert 08:48 PM time into SQL unixtime?

How do I convert 08:48 PM formatted string into SQL Unixtime?
Java 1.5
You need java.text.SimpleDateFormat with the hh:mm a pattern (0-12 hours, minutes, AM/PM marker). Click the link to see the Javadoc with detailed pattern explanations.
String time = "08:48 PM";
Date date = new SimpleDateFormat("hh:mm a").parse(time);
long timestampMillis = date.getTime();
long unixTimestamp = timestampMillis / 1000;
If you actually want to store this in a SQL TIME/TIMESTAMP/DATETIME field with help of JDBC, then wrap it in a java.sql.Time and use PreparedStatement#setTime() to save it.
Time time = new Time(timestampMillis); // Yes, with millis!
preparedStatement.setTime(1, time);
// ...
Assuming you still want today's date, try
String today = new SimpleDateFormat("yyyy-MM-dd ").format(new Date());
long timestamp = new SimpleDateFormat("yyyy-MM-dd hh:mm a").parse(today + "08:48 PM").getTime() / 1000;

Categories