Java MYSQL Timestamp with millieconds issue [duplicate] - java

This question already has answers here:
Timestamp with a millisecond precision: How to save them in MySQL
(4 answers)
Closed 2 years ago.
I am using MySQL and have a column type TimeStamp. I am not able to store or retrieve milliseconds.
Any help please ? Thanks!
public static Date getBeginAndEndTime(String time) {
DateTimeFormatter format = DateTimeFormatter.ofPattern("HHmmssSSS");
LocalTime lt = LocalTime.parse(time, format);
return Timestamp.valueOf(lt.atDate(LocalDate.now()));
}
Ex: Time value coming as is "140833222" and when I stored in database it looks like "2020-08-26 14:08:33" missing last milliseconds "222".
I need to store and retrieve including milliseconds.

You should use the datatype BIGINT as it allows you to store 8 bytes. The DATE field is generally used for just that, Date and DateTime implementations like LocalDateTime. Since you're using a LocalDate and subsequently a LocalTime you're better off just using a DATE type. Your method effectively becomes;
public static Date getBeginAndEndTime(String time) {
// cache this, it's a thread-safe object
DateTimeFormatter format = DateTimeFormatter.ofPattern("HHmmssSSS");
return LocalTime.parse(time, format).atDate(LocalDate.now()));
}

Related

How to convert UTC timestamp to asia/jakarta time [duplicate]

This question already has answers here:
Convert UTC date to current timezone
(5 answers)
Timezone conversion
(13 answers)
Closed 2 years ago.
I have string timestamp like
2020-05-25 08:03:24
I have tried to split the String using " " (a whitespace) as delimiter to get two Strings "2020-05-25" and "08:03:24". After that, I used substring to get the hours and added 7 to have jakarta time.
But when it is 17:01:00 for example, my calculated date is wrong.
The date given is in UTC.
I want to convert it become timezone [ASIA/Jakarta] how to convert utc timestamp become asia jakarta time?
You can use java.time if you are using Java 8 or higher.
The library provides handy possibilities of converting datetimes that don't have information about a time zone (like your example String) to a zone and handle conversions from one zone to another.
See this example:
public static void main(String[] args) {
// datetime string without a time zone or offset
String utcTimestamp = "2020-05-25 08:03:24";
// parse the datetime as it is to an object that only knows date and time (no zone)
LocalDateTime datetimeWithoutZone = LocalDateTime.parse(utcTimestamp,
DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
// convert it to a zone-aware datetime object by adding a zone
ZonedDateTime utcZdt = datetimeWithoutZone.atZone(ZoneId.of("UTC"));
// print the datetime in utc once
System.out.println(utcZdt);
// then convert the zoned datetime to a different time zone
ZonedDateTime asiaJakartaZdt = utcZdt.withZoneSameInstant(ZoneId.of("Asia/Jakarta"));
// and print the result
System.out.println(asiaJakartaZdt);
}
The output is
2020-05-25T08:03:24Z[UTC]
2020-05-25T15:03:24+07:00[Asia/Jakarta]

Java convert utc time to Local time [duplicate]

This question already has answers here:
Java: How do you convert a UTC timestamp to local time?
(3 answers)
Closed 5 years ago.
Hi I have a problem : from server I get a time and it looks like this :
"date":"2017-05-24T07:56:22Z"
But now in my local time is 09:56:22 how I can convert this ?
First you need to parse the date, for example:
Instant instant = Instant.parse("2017-05-24T07:56:22Z");
Assuming your time zone is correctly set, you can then simply use:
LocalTime localTime = instant.atZone(ZoneId.systemDefault()).toLocalTime();
If you want to use a specific time zone instead of the system default time zone:
LocalTime localTime = instant.atZone(ZoneId.of("Europe/London")).toLocalTime();

Store date as yyyy-MM-dd in database without time? [duplicate]

This question already has answers here:
Java Date - Insert into database
(10 answers)
Closed 6 years ago.
How can I best store a String with format yyyy-MM-dd (without time declaration) in SQL database (postgres)?
I later want to use that String always as Date type. I also want to execute query against the database to give me records that are before or after that Date.
Should I store it as a String or as a Date type in DB?
If I store it as a Date, in database I see yyyy-MM-dd HH:mm:ss. How could I prevent the time declaration?
If you do not want to store a time component, then use the DATE data type. It does not have a time or a time zone component, so is useful for dates of birth, dates of employment start/end, and other data for which the time is not relevant.
http://www.postgresql.org/docs/9.3/static/datatype-datetime.html
The display format is a matter for the application -- just use the correct data type. YYYY-MM-DD is documented as the best format for suplying dates, though.
Always recommended one is Date with time-stamp. If you don't need then while storing store it as 00:00:00.(Use Sql Date for date without time-stamp.)
Use business logical in order to truncate the time and the format you required. Service layer you can play with date and in most of DB its better to store Date with timestamp.

Convert epoch String to Java date [duplicate]

This question already has answers here:
Unix epoch time to Java Date object
(7 answers)
Closed 9 years ago.
I am getting an epoch String from my DB which looks something like this : 1391328000000
I am having a hard time trying to convert it to Java Date.
I tried the following :
private String buildDate(String dateString){
System.out.println("dateString " + dateString);
DateFormat format = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
format.setTimeZone(TimeZone.getTimeZone("Etc/UTC"));
String formatted = format.format(Integer.parseInt(dateString));
return formatted;
}
I think you're overthinking about the DateFormat. If I want to simply obtain a Date instance, what I would try is the following:
Date d = new Date(Long.parseLong(dateString));
You need to turn it into a java.util.Date object in order for SimpleDateFormat to handle it. Also, a value like what you quoted needs to be parsed as a long, as it is too large for an int.
That is, change the line where you set formatted to be:
String formatted = format.format(new Date(Long.parseLong(dateString)));
As an aside, if the project you're working on can handle an extra external dependency, switch date/time handling over to the joda library. The stuff in java.util (that is, Date and Calendar) rapidly becomes painful and error-prone to work with.

difference between util.date and sql.date? [duplicate]

This question already has answers here:
Difference between Date class in Package java.util & Package java.sql
(5 answers)
java.util.Date vs java.sql.Date
(7 answers)
Closed 9 years ago.
Can many one help me?
import java.util.Date;
public class DateDemo {
public static void main(String [] p)
{
java.util.Date date1 = new Date();
#SuppressWarnings("deprecation")
java.sql.Date date2 = new java.sql.Date(2013, 12, 22);
System.out.println(date1.compareTo(date2));
}
}
As per Javadoc java.sql.Date is a thin wrapper around millisecond value which is used by JDBC to identify an SQL DATE type.
java.sql.Date just represent DATE without time information while java.util.Date represent both Date and Time information. This is the major differences why java.util.Date can not directly map to java.sql.Date.
In order to suppress time information and to confirm with definition of ANSI SQL DATE type, the millisecond values used in java.sql.Date instance must be "normalized by setting the hours, minutes, seconds and milliseconds to zero in the timezone with with DATE instance is associated. In other words all time related information is removed from java.sql.Date class.
Read more: http://javarevisited.blogspot.com/2012/04/difference-between-javautildate-and.html#ixzz2bBWDwBD0
Date from util package has is combination of date and time while Date from SQL package only represent only Date part. to be precise Date contains year, month and day information while Time means hour, minute and second information. java.util.Date contains all year, month, day, hour, minute and second information. In fact java.sql.Time and java.sql.TimeStamp which represents TIME and TIMESTAMP type of SQL database is more close to java.util.Date, It extends java.util.DATE and if you are using java.util.DATE in your Class to represent DATE value its better to use TIMESTAMP type in Database and java.sql.Time in JDBC or DAO code.
Read more: http://javarevisited.blogspot.com/2012/04/difference-between-javautildate-and.html#ixzz2bBmF4lBd

Categories

Resources