Why is getTime() having an error? I have tried everything, but cannot figure out what the problem is. As far as I know, I have converted the String arrayOpportunity[2] into a date. (It was originally a String.) Thanks!
SimpleDateFormat df = new SimpleDateFormat();
df.applyPattern("yyyy-MM-dd HH:mm:ss");
// Calendar timestamp = Calendar.getInstance();
// timestamp.setTime(df.parse(arrayOpportunity2)[0]);
arraySuspects.add(arrayGPS[0]);
// }
// long timediff = coord2.getTimestamp().getTimeInMillis() -
// coord1.getTimestamp().getTimeInMilis();
Date convertedDate = df.parse(arrayOpportunity[2]);
Date duration = df.parse("0000-00-00 00:14:59");
Date lastTime = df.parse(arrayOpportunity[2]);
// SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/mm/dd");
System.out.println(arrayOpportunity[2]);
// arrayOpportunity[2].setTime(arrayOpportunity[2].getTime() + duration);
// lastTime += duration;
arrayOpportunity[2].setTime(arrayOpportunity[2].getTime() + (((14 * 60) + 59)* 1000));
Calling df.parse(arrayOpportunity[2]); does not convert arrayOpportunity[2] to a Date, it assigns that value to lastTime. In your code call lastTime.getTime() instead of arrayOpportunity[2].getTime() as arrayOpportunity[2] is still a String
// Create your date parser
SimpleDateFormat df = new SimpleDateFormat();
// Set the date pattern
df.applyPattern("yyyy-MM-dd HH:mm:ss");
// Create a Date object by parsing the value of arrayOpportunity[2]
Date lastTime = df.parse(arrayOpportunity[2]);
// Set a new value to the Date object by performing a calculation on the result of getTime()
lastTime.setTime(lastTime.getTime() + (((14 * 60) + 59)* 1000));
Related
I have two date time string, one is current time and second is given as follows.
String currentTime = "05/30/2018 16:56:21";
String endTime = "05/30/2018 16:59:21";
Now I want to check if the endTime has passed currentTime.
Thanks
Take a look at
this and this
Example:
String currentTime = "05/30/2018 16:56:21";
String endTime = "05/30/2018 16:59:21";
SimpleDateFormat sdf = new SimpleDateFormat("dd/mm/yyyyHH:mm:ss");
try {
Date currentTimeDate = sdf.parse("05/30/2018 16:56:21");
Date endTimeDate = sdf.parse("05/30/2018 16:59:21");
currentTimeDate.compareTo(endTimeDate); // false / current time has not passed end time.
endTimeDate.compareTo(currentTimeDate); // true / end time has passed current time.
} catch (ParseException ignored) {
}
Convert both strings to Date object and then use before() method to check if the end time has passed currentTime.
String currentTime = "05/30/2018 16:56:21";
String endTime = "05/30/2018 16:59:21";
Date current=new SimpleDateFormat("dd/MM/yyyy").parse(currentTime);
Date end=new SimpleDateFormat("dd/MM/yyyy").parse(endTime);
if(end.before(current)) {
// end time has passed currenctTime
} else {
// no
}
Keep both times in milliseconds which is a long value
long currentTime= System.currentTimeMillis();
You can also convert your and time in millies using below code.
String givenDateString = "Tue Apr 23 16:08:28 GMT+05:30 2013";
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
try {
Date mDate = sdf.parse(givenDateString);
long endTime= mDate.getTime();
System.out.println("Date in milli :: " + endTime);
} catch (ParseException e) {
e.printStackTrace();
}
Now compare, if current time is larger then end time, thus current time has passed end time like below.
if(currentTime>endTime){
//Do stuff
}
Enjoy..
I am converting timestamp to date format yyyy-MM-dd HH:mm:ss.SSS and I am using America/New_York as TimeZone. Whenever I convert the timestamp into the date it shows one hour less than usual date and time. How to resolve this in java?
Here's the code:
String timestamp = "1431941838000";
long time = Long.valueOf(timestamp);
Date currentDate = new Date(time);
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
TimeZone zone = TimeZone.getTimeZone("America/New_York");
df.setTimeZone(zone);
String finale = df.format(currentDate);
Try to using EST to replace America/New_York like
TimeZone zone = TimeZone.getTimeZone("EST");
Updated
It's My Test code:
String timestamp = "1431941838000";
long time = Long.valueOf(timestamp);
Date currentDate = new Date(time);
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
TimeZone zoneNewYork = TimeZone.getTimeZone("America/New_York");
df.setTimeZone(zoneNewYork);
String finale = df.format(currentDate);
System.out.println(finale);
TimeZone zoneEst = TimeZone.getTimeZone("EST");
df.setTimeZone(zoneEst);
finale = df.format(currentDate);
System.out.println(finale);
And My result as bellow:
2015-05-18 05:37:18.000
2015-05-18 04:37:18.000
You have an extra point in this line:
TimeZone zone = TimeZone.getTimeZone("America/New_York").;
// ^ here!!!
UPDATE if you dont get any error, the output must be the correct:
EST is UTC - 5 hours. America/New_York is EST in the winter and E*D*T in the summer, so check if String timestamp = "1431941838000"; is winter or summer...
This code works ok:
Calendar calNewYork = Calendar.getInstance();
calNewYork.setTimeZone(TimeZone.getTimeZone("America/New_York"));
System.out.println("Time in New York: " + calNewYork.get(Calendar.HOUR_OF_DAY) + ":"
+ calNewYork.get(Calendar.MINUTE));
Use this to check your time:
long timestamp = "1431941838000";
Calendar calNewYork = Calendar.getInstance();
calNewYork.setTimeZone(TimeZone.getTimeZone("America/New_York"));
calNewYork.setTime(new Date(timestamp));
System.out.println("Time in New York: " + calNewYork.get(Calendar.HOUR_OF_DAY) + ":"
+ calNewYork.get(Calendar.MINUTE));
This question already has answers here:
Change date format in a Java string
(22 answers)
Closed 8 years ago.
I have a object that giving date and time in this format "2014-06-11 16:32:36.828".
I want to remove millisec .828.
In my db that object is in time stamp format but whenever i am showing i am converting it to tostring().
so how to remove millisec please help me
The following code convert "2014-06-11 16:32:36.828" into "2014-06-11 16:32:36"
new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").parse("2014-06-11 16:32:36.828"));
Explanation:
new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").parse("2014-06-11 16:32:36.828") parse the input string into
Wed Jun 11 16:32:36 IST 2014
new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) format the input date into specified structure.
I would use DateUtils.truncate(date, Calendar.SECOND)
Date d = SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(yourString);
Calendar c = Calendar.getInstance();
c.setTime(d);
c.set(Calendar.MILLISECOND, 0);
return c.getTime();
I remember there is a way to directly read Date off your timestamp field but I don't do that in my everyday coding. So I'd left for others to post so. Nevertheless, you can use the same above code to translate your date from that timestamp into a date without MILLISECOND.
If you receive it as a Timestamp, you should use the appropriate formatter when converting it to a string:
String s = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(timestamp);
Note: this will use the default time zone of the local computer.
Extract epoch millis from the original Date object and do integer division by 1000 followed by multiplication by 1000. Create Date object with the time zone of the original object and the millis calculated the above suggested way.
You can get the system time as follows without milliseconds
import java.text.SimpleDateFormat;
import java.util.Calendar;
And the code
Calendar currentDate = Calendar.getInstance();
SimpleDateFormat formatter= new SimpleDateFormat("dd-MM-YYYY-hh:mm:ss");
String dateNow = formatter.format(currentDate.getTime());
System.out.println(dateNow);
if you want to mantain the format try something like that:
public static String getFechaTimestampToString (Timestamp timestamp) {
String date = "";
Calendar cal = Calendar.getInstance();
cal.setTime(new Date(timestamp.getTime()));
int year = cal.get(Calendar.YEAR);
int month = cal.get(Calendar.MONTH)+1;
int day = cal.get(Calendar.DAY_OF_MONTH);
String monthstr = "";
String daystr = "";
if(month<10)
monthstr = "0"+month;
else
monthstr = ""+month;
if(day<10)
daystr = "0"+day;
else
daystr = ""+day;
date = year + "-" + monthstr + "-" + daystr ;
return date;
}
To reverse data to database:
public static Timestamp getFechaStringToTimestamp (String strDate) {
Timestamp timestamp = null;
strDate = strDate + " 00:00:00";
timestamp = Timestamp.valueOf(strDate);
return timestamp;
}
I want to calculate the difference in minutes of two time stamp variables(endTime_check and endtime) .. first variable endTime_check is declared as String and Second variable endtime is taken from database type is DateTime.
code is :
DateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");
String endTime_check = "";
endTime_check = timeFormat.format(cal.getTime());
//endtime is taken from database, type is DateTime
endtime = rs.getString("endtime");
Something like this:
Date endTime = rs.getDate("endtime");
String endTimeCheck = "12:34:56";
DateFormat df = new SimpleDateFormat("HH:mm:ss");
long endTimeCheckMillis = df.parse(endTime).getTime();
long endTimeMillis = endTime.getTime();
long differenceMinutes = Math.abs(((endTimeCheckMillis - endTimeMillis) / 60000));
If you want a signed difference leave out the 'Math.abs' call.
import java.util.*;
import java.text.*;
public class GetPreviousAndNextDate
{
public static void main(String[] args)
{
int MILLIS_IN_DAY = 1000 * 60 * 60 * 24;
Date date = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yy");
String prevDate = dateFormat.format(date.getTime() - MILLIS_IN_DAY);
String currDate = dateFormat.format(date.getTime());
String nextDate = dateFormat.format(date.getTime() + MILLIS_IN_DAY);
System.out.println("Previous date: " + prevDate);
System.out.println("Currnent date: " + currDate);
System.out.println("Next date: " + nextDate);
}
}
i have this error
(Error(9,32): method format(long) not found in class java.text.SimpleDateFormat )
Your code's logic is wrong. The results will be an hour off around the Daylight Savings Time switch, because that involves days that are 23 or 25 hours long.
For date arithmethic, you should always use the Calendar class:
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DAY_OF_MONTH, -1);
String prevDate = dateFormat.format(cal.getTime());
cal.add(Calendar.DAY_OF_MONTH, 2);
String nextDate = dateFormat.format(cal.getTime());
(note that Calendar.getTime() returns a Date object and thereby fixes the type error as well)
In order to create a date from a long you simply have to use the new Date(long) API:
new Date(date.getTime() - MILLIS_IN_DAY);
These lines use a method which doesn't exist:
String prevDate = dateFormat.format(date.getTime() - MILLIS_IN_DAY);
String currDate = dateFormat.format(date.getTime());
String nextDate = dateFormat.format(date.getTime() + MILLIS_IN_DAY);
Method format accepts Date objects as parameters.
Try this:
String prevDate = dateFormat.format(new Date(date.getTime() - MILLIS_IN_DAY));