I have wierdo problem with timestamp in Java/Android
Date inputDate = null;
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm dd/MM/yyyy");
sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
Log.e("DateH:D", hour+" "+day);
try {
inputDate = sdf.parse(hour + " " + day);
Date currentDate = new Date();
Log.e("InputDate", inputDate.toString());
Log.e("InputDate",inputDate.getTime()+"");
Log.e("CurrentDate", currentDate.toString());
Log.e("CurrentDate",currentDate.getTime()+"");
if (!inputDate.after(currentDate) ){
//TODO change this string
hourField.setError("Date from past");
return false;
}
} catch (ParseException e) {
Log.e("DateParser" , e.getLocalizedMessage);
return false;
}
Example output for this is:
DateH:D: 12:33 15/09/16
E/InputDate: Tue Sep 15 14:33:00 CET 16
E/InputDate: -61640134020000
E/InputDate: Tue Sep 15 14:33:00 CET 16
E/CurrentDate: Thu Sep 15 11:38:43 CEST 2016
E/CurrentDate: 1473932323198
So non-timestamp representation of date is correct but timestamp is wrong. How it's possible? What i'm doing wrong?
Use yy to parse 2-digit years.
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm dd/MM/yy");
The getTime method of Date:
Returns the number of milliseconds since January 1, 1970, 00:00:00 GMT
represented by this Date object.
Since the year you specified is year 16, the negative result makes sense.
The starting date for epoch milis is Thu, 01 Jan 1970 00:00:00 GMT. Any datetime before that is negative in epoch milis.
Related
I am converting from epoch time (which is in UTC) to a format as shown below. Now I tried different SO answers to convert UTCDate from UTC to local time. But I am not getting the local time.
Any help would be appreciated.
String epochTime = "1436831775043";
Date UTCDate = new Date(Long.parseLong(epochTime));
Date localDate; // How to get this?
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("h:mm a");
String result = simpleDateFormat.format(UTCDate);
Also, the conversion has to be done without the help of any external library.
Java 8
String epochTime = "1436831775043";
Instant utcInstant = new Date(Long.parseLong(epochTime)).toInstant();
ZonedDateTime there = ZonedDateTime.ofInstant(utcInstant, ZoneId.of("UTC"));
System.out.println(utcInstant);
LocalDateTime here = there.withZoneSameInstant(ZoneId.systemDefault()).toLocalDateTime();
System.out.println(here);
Which outputs:
2015-07-13T23:56:15.043Z
2015-07-14T09:56:15.043
After thoughts...
I think you're chasing your tail. Date is just a container for the number of milliseconds since the epoch (January 1, 1970, 00:00:00 GMT). It doesn't internally carry a representation of a time zone (AFAIK).
For example...
String epochTime = "1436831775043";
Date UTCDate = new Date(Long.parseLong(epochTime));
// Prints the "representation" of the Date
System.out.println(UTCDate);
// Local date/time format...
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd/MM/yyyy h:mm:ss a");
try {
System.out.println("local format: " + simpleDateFormat.format(UTCDate));
System.out.println("local Date: " + simpleDateFormat.parse(simpleDateFormat.format(UTCDate)));
} catch (ParseException ex) {
Logger.getLogger(JavaApplication203.class.getName()).log(Level.SEVERE, null, ex);
}
// UTC date/time format
try {
simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
System.out.println("utc format: " + simpleDateFormat.format(UTCDate));
System.out.println("utc date: " + simpleDateFormat.parse(simpleDateFormat.format(UTCDate)));
} catch (ParseException ex) {
Logger.getLogger(JavaApplication203.class.getName()).log(Level.SEVERE, null, ex);
}
Which outputs...
Tue Jul 14 09:56:15 EST 2015
local format: 14/07/2015 9:56:15 AM
local Date: Tue Jul 14 09:56:15 EST 2015
utc format: 13/07/2015 11:56:15 PM
utc date: Tue Jul 14 09:56:15 EST 2015
If you have a look at local Date and utc date they are the same thing, even though the local format and utc format are formatted correctly.
So, instead of chasing your tale trying to get Date to "represent" a value you want, either use Java 8's Time API or JodaTime to manage the Time Zone information or simply format the Date into the Time Zone you want...
Further, if we do something like...
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd/MM/yyyy h:mm:ss a");
Date localDate = simpleDateFormat.parse(simpleDateFormat.format(UTCDate));
simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
Date utcDate = simpleDateFormat.parse(simpleDateFormat.format(UTCDate));
System.out.println(localDate.getTime());
System.out.println(utcDate.getTime());
System.out.println(localDate.equals(utcDate));
It will print...
1436831775000
1436831775000
true
You can set your time zone in the formatter:
simpleDateFormat.setTimeZone(TimeZone.getDefault());
I am new to java. I have a Date that is stored in the variable, pubDate = "2013-09-23"
When I'm executing this
SimpleDateFormat pubSimpleDateFormat = new SimpleDateFormat("yyyy-mm-dd");
Date publishDate = pubSimpleDateFormat.parse(pubDate);
I'm getting wrong value : Wed Jan 23 00:09:00 GMT+05:30 2013
Please help me why it so. and help me to solve this.
M is for Month in year while m is for Minute in hour
You should use SimpleDateFormat pubSimpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
String pubDate = "2013-09-23";
SimpleDateFormat pubSimpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date publishDate = pubSimpleDateFormat.parse(pubDate);
System.out.println(publishDate);
Output :
Mon Sep 23 00:00:00 GMT 2013
Read the section Date and Time Patterns.
DateFormat formatter = new SimpleDateFormat("yy-mm-dd");
formatter.setLenient(false);
String[] dateStr = { "2013-12-27", "2013-01-03"};
for (int i = 0; i <= 1; i++) {
Date date = formatter.parse(dateStr[i]);
System.out.println("date is "+date);
}
result :
Sun Jan 27 00:12:00 IST 2013
Thu Jan 03 00:01:00 IST 2013
i am parsing string date in to Date.but it is giving me date Starting with month Jan regardless of what month i am passing to formatter constructor.
The format for your date would be yy-MM-dd. Update your format and check.
mm for minutes
MM for month
Use: "yy-MM-dd"
See here
once Silly Mistake
DateFormat formatter = new SimpleDateFormat("yy-MM-dd");
Format this line in your code
I'm trying to parse a date from a String and get the long value. The long value will be later sent to an SQL query.
here's my code:
String dayDate = "28-02-2013";
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
Date day = new Date();
try {
day = sdf.parse(dayDate);
} catch (ParseException pe) {
pe.printStackTrace();
}
System.out.println("day : "+day.toString()+ " long : " + day.getTime());
which gives the following output:
day : Thu Feb 28 00:00:00 EET 2013 long : 1362002400000
which is correct but not what I want since the long value results in Wed, 27 Feb 2013 22:00:00 GMT (http://www.epochconverter.com/) (I'm in a GMT+2 timezone). And i need to send to correct long value to sql.
Is there anyway to work around this without using external libraries?
SimpleDateFormat is locale-aware, meaning the date it parses is in your timezone. Midnight 28 Feb in GMT+2 is actually 10pm 27 Feb in GMT, the long value 1362002400000. I would add this to get the parsing right (would't bother using Calendar):
sdf.setTimeZone(TimeZone.getTimeZone("GMT"))
Again, when you print this date it uses SimpleDateFormat and that's why you can see EET in the output.
Passing this to database is a different story though once you get this right.
Use DateFormat.setCalendar(Calendar cal) to set a Calendar with GMT as its timezone, or use DateFormat.setTimeZone(TimeZone zone) with the GMT TimeZone. That will ensure that the resulting Date will be 00:00:00 in GMT instead of in EET.
If you add a timezone specifier to your string you can force java to use GMT for the conversion:
String dayDate = "28-02-2013";
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy z"); // z is a timezone specifier
Date day = new Date();
try {
day = sdf.parse(dayDate + " GMT"); // Use GMT timezone.
} catch (ParseException pe) {
pe.printStackTrace();
}
System.out.println("day : "+day.toString()+ " long : " + day.getTime());
You are converting between text and internal (Date) representations of dates and times without explicitly stating the time-zone. That never goes well.
Calendar calendar = Calendar.getInstance();
calendar.setTimeZone(TimeZone.getTimeZone("America/Los_Angeles"));
Date date = calendar.getTime();
Use your timezone String:
TimeZones
Trying to compare some dates in java but can't get the formatting right, where am i going wrong?
DateFormat df = new SimpleDateFormat("dd/mm/yyyy");
Date date1 = null, date2 = null, today = new Date();
date1 = (Date) df.parse(scan.next());
System.out.println(date1);
System.out.println(today);
if(date1.compareTo(today) < 0){
date1 = null;
System.out.println(start + " is not a valid date.. please try again!");
}
Please enter a start date:
10/04/2011
Mon Jan 10 00:04:00 GMT 2011
Tue Apr 05 22:27:44 BST 2011
I think you need MM, not mm
From the doc:
M Month in year
m Minute in hour
Change line 1 to be:
DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
mm in SimpleDateFormat is the minutes. MM is the month. So your input is actaully January 10 2011 at 00:10:00
Check out http://download.oracle.com/javase/1.4.2/docs/api/java/text/SimpleDateFormat.html for abbreviations and javadoc.