Same date different long time - java

i've a object Date as for example this date "Fri Aug 01 00:00:00 CDT 2014" that through this validation:
Date fechaInicio = filtros.containsKey("dateFechaInicio") ? Fecha.getMinDate(Fecha.getFecha(filtros.get("dateFechaInicio").toString())) : Fecha.getMinDate(null);
Where map filtros contains the key "dateFechaFinal" it will to method Fecha.getMaxDate(Date date) that does
public static Date getMinDate(Date fecha) {
Calendar fechaMin = Calendar.getInstance();
fechaMin.setTime(fecha != null ? fecha : new Date());
fechaMin.set(fechaMin.get(Calendar.YEAR),
fechaMin.get(Calendar.MONTH),
fecha != null ? fechaMin.get(Calendar.DAY_OF_MONTH) : fechaMin.getActualMinimum(Calendar.DAY_OF_MONTH),
fechaMin.getActualMinimum(Calendar.HOUR_OF_DAY),
fechaMin.getActualMinimum(Calendar.MINUTE),
fechaMin.getActualMinimum(Calendar.SECOND));
return fechaMin.getTime();
}
And return a new Date, if this method receives a date return this Long time 1406869200806 , but if receives a date equal to null the method takes the first day from month for example "Fri Aug 01 00:00:00 CDT 2014" but now return this Long time 1406869200000..
"filtros obtained a String that method getFecha() convert to new Date"
Why happen it?

You're only setting values down to the second, so the millisecond part is left at whatever it was from new Date(). Your description isn't entirely clear, but I suspect you just want:
fechaMin.set(Calendar.MILLISECOND, 0);
Alternatively, use a better API, such as Joda Time or the java.time API in Java 8.

Related

Converting date to GMT

I want to convert a date to GMT.
I get a date in BST, I want to convert it to GMT without time zone conversion.
Example:
**If the BST date is: Wed June 26 13:30:13 BST 2019
I want to convert it to Wed 26 Jun 2019 13:30:13 GMT**
I want to ignore the timezone info and return the same date as GMT.
For this I am trying
private SimpleDateFormat dateFormatLocal = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
private SimpleDateFormat dateFormatGmt = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
private SimpleDateFormat dateFormatGmtText = new SimpleDateFormat("EEE dd MMM yyyy HH:mm:ss 'GMT'");
dateFormatGmt.setTimeZone(TimeZone.getTimeZone("GMT"));
String textDate = dateFormatLocal.format(date);
//Date is Wed June 26 13:30:13 BST 2019
private Date toGMTDate(final Date date) {
String textDate = dateFormatLocal.format(date);
try {
String[] dateParts = textDate.split("\\+");
textDate = dateParts[0] + "+0000";
return dateFormatGmt.parse(textDate);
} catch (ParseException e) {
return null;
}
}
private String toGMT(final Date date) {
return dateFormatGmtText.format(toGMTDate(date));
}
When I call toGMT it returns Wed 26 Jun 2019 14:30:13 GMT
I am not sure why it is so?
What is wrong here?
java.time
You said you can’t use the modern date and time API, but for other readers I should like to present that option first. SimpleDateFormat and Date are poorly designed and long outdated, the former in particular notoriously troublesome, so I recommend avoiding them.
I am assuming that BST is for British Summer Time (other interpretations exist). And I am assuming that you cannot avoid getting an old-fashioned Date object.
private static DateTimeFormatter formatter
= DateTimeFormatter.ofPattern("EEE dd MMM yyyy HH:mm:ss z", Locale.UK);
private static ZoneId britain = ZoneId.of("Europe/London");
private static ZoneId gmt = ZoneId.of("Etc/GMT");
private static String toGMT(final Date date) {
ZonedDateTime britishTime = date.toInstant().atZone(britain);
ZonedDateTime gmtTime = britishTime.withZoneSameLocal(gmt);
return gmtTime.format(formatter);
}
Try it out with your Date of Wed Jun 26 13:30:13 BST 2019:
String textDate = dateFormatLocal.format(date);
System.out.println(textDate);
System.out.println(toGMT(date));
Output is:
2019-06-26T13:30:13+0100
Wed 26 Jun 2019 13:30:13 GMT
Whenever you get an old-fashioned Date, the first thing to do is to convert it to Instant. Then do any further conversions from there. The key to changing time zone and keeping the date and time of day (hour-minute-second of day) is the withZoneSameLocal method of the ZonedDateTime class.
I recommend specifying locale for the formatter.
I am not sure why it is so? What is wrong here?
A Date hasn’t got, as in cannot have a time zone. It’s a point in time, nothing more. YourtoGMTDate method returns a point in time that is an hour later: The time you gave it was 13:30:13+0100, and it returned 13:30:13+0000, which is the same point in time as 14:30:13+0100. Next you formatted this point in time using a formatter that used your default time zone, Europe/London, and therefore produced 14:30:13, but at the same time printed GMT in the string — the result you reported.
…the new time library, but for some reasons I can't use them.
If you really have got an evil boss that either forces you to use Java 1.4 or 1.5 and/or forbids the use external dependencies, the pretty simple hack is:
private String toGMT(final Date date) {
return dateFormatGmtText.format(date);
}
The cheating is: Your dateFormatGmtText uses your default time zone, Europe/London, but lies and prints GMT in the formatted string. This gives the same output as above — the output you asked for. Compared to your code I am just leaving out the date conversion.
Link: Oracle tutorial: Date Time explaining how to use java.time.

Unix timestamp keeps returning Jan 17 1970 in DateTime

I am using the following method to return a formatted date as say 07:00AM, Apr 12 2016. But I keep getting 01:41PM, Sat, Jan 17 1970. Say for example my timestamp is 1460469600.
Here is my method.
public static String formattedDate(long timestamp) {
DateTime date = new DateTime(timestamp);
String formatted= date.toString("hh:mma, EEE, MMM dd yyyy");
return formatted;
}
Your timeStamp is wrong. It doesnt represent the correct time in millis. YOur timeStamp refers to 01:41PM, Sat, Jan 17 1970.
You can check what time date the timeinmillis (TimeStamp) refers to from this site.
http://currentmillis.com/
To get the correct time from unix time stamp just change your DateTime date = new DateTime(timestamp); into
DateTime date = new DateTime(timestamp*1000);
Because unix time gives timpestamp in seconds and we need millis here.
Query to get timestamp in milliseconds :
select UNIX_TIMESTAMP(yourtimestamp) *1000 from tablename.
this gives time stamp in milliseconds in mysql

convert string to german date java

my problem is the following. I would like to have the german date for the string "11.11.2012".
I tried this piece of code:
String date = "11.11.2012";
SimpleDateFormat sdtF = new SimpleDateFormat("dd.mm.yyyy",Locale.GERMANY);
Date dareFormatiert = sdtF.parse(date);
System.out.println(dareFormatiert);
But it gives me the wrong format. "Wed Jan 11 00:11:00 CET 2012", instead of "11.11.2012".
Thank you, any help is appreciated.
You have to use M in you pattern, because M is the month and m is the minute!
String date = "11.11.2012";
SimpleDateFormat sdtF = new SimpleDateFormat("dd.MM.yyyy",Locale.GERMANY);
^^^^
Date dareFormatiert = sdtF.parse(date);
System.out.println(dareFormatiert);
For more information see the documentation of SimpleDateFormat
but i still have the same output: Sun Nov 11 00:00:00 CET 2012
Try to understand the thing when you use
SimpleDateDFormat#parse() - It parses text from a string to produce a Date.
and Date object in java always contains date along the time.
Javadoc says Date() - Allocates a Date object and initializes it so that it represents the time at which it was allocated, measured to the nearest millisecond.
and FYI Sun Nov 11 00:00:00 CET 2012 is equal to 11.11.2012
Edit: try this
public static Date convertUtilDateToSqlDate(java.util.Date date){
if(date != null && !(date.equals(""))) {
java.sql.Date sqlDate = new java.sql.Date(date.getTime());
return sqlDate;
}
return null;
}
pass the util date you got above and this method shall return the sql format date then store the sqlformat date in mysql column-field of type Date

Parse a string to date in specific format

I have a TimeStamp '2013-06-24 10:46:11.0' and I need to cut off the .0 part, so what I did was to use the SimpleDateFormat to parse it to String and back then parse it to date, the first conversion was fine but the second (string to date) throws a java date time.
public void convert(Object object) {
Date date;
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
date = object().getDate();
String formated = format.format(date);
try {
date = format.parse(formated);
} catch (ParseException ex) {
Logger.getLogger(DlgConsultaFactura.class.getName()).log(Level.SEVERE, null, ex);
}
}
What I expect is a date like this 2013-06-24 10:46:11, but what I got is this date Mon Jun 24 10:46:11 CDT 2013
Any help will be appreciated. Thanks.
Mon Jun 24 10:46:11 CDT 2013 and 2013-06-24 10:46:11 is actually same value. Mon Jun 24 10:46:11 CDT 2013 is as per your default locale.
You're getting confused between date's internal representation and its display format.
To print in 2013-06-24 10:46:11 you can use same SimpleDateFormat object again.
You can use DateFormat#format(Date) to print the date or return the String representation in your desired format i.e. "yyyy-MM-dd HH:mm:ss". Something like this:
String myDt = format.format(date);
// 2013-06-24 10:46:11
Not the best way but a quick and easy one if you want just the string representation of the date ...
formated = formated.substring(0, formated.length()-2);
:)
DateFormat i.e. SimpleDateFormat just formats the date as per your need.
Parse returns the Date representation of the String (or timestamp in your case) passed in.
Format returns the String representation of the Date object passed in.
In both the cases , you see the same date just the representation is different.

Date formatting issue in java

When I parsing time in java, I passing "12:12" as string argument, then I am getting "Thu Jan 01 12:12:00 IST 1970" as a output.
I want current year like "Fri Mar 09 12:12:00 IST 2012" as a output.
String timestr = "12:12";
Date convertedDate = null;
try {
SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm");
convertedDate = dateFormat.parse(timestr);
} catch (ParseException e) {
e.printStackTrace();
}
System.out.println(convertedDate);
Thanks!
I think that the problem with this is that you are creating a date with null values and then just initialize the time value. I think you should use the Calendar class and get an instance of the Calendar and then set the time. Once that is done, you create a date object from the Calendar and parse it to your needs.

Categories

Resources