Skip to content

Instantly share code, notes, and snippets.

@hendrasyp
Last active February 28, 2020 08:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hendrasyp/6d5a7b92bbbf2fa1667cb7e8501113fb to your computer and use it in GitHub Desktop.
Save hendrasyp/6d5a7b92bbbf2fa1667cb7e8501113fb to your computer and use it in GitHub Desktop.
Formatting java.sql.Timestamp
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.sql.Date;
import java.sql.Timestamp;
@AllArgsConstructor
@NoArgsConstructor
@Data
public class Foo {
public Timestamp EditDate;
public String StrEditDate;
}
import java.sql.Date;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
public class MyClass {
public static void main(String args[]) {
// Jika dapat dari Database
// Foo foo = new Foo()
// Timestamp oStamp = foo.getEditDate();
SimpleDateFormat outputFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
// Timestamp timestamp = Timestamp.valueOf(oStamp.toString());
Timestamp timestamp = Timestamp.valueOf("2020-02-25 05:52:56.000000");
Long time = timestamp.getTime();
Timestamp ts = new Timestamp(time);
String convertedDate = outputFormat.format(ts);
// foo.setStrEditDate(convertedDate);
System.out.println(timestamp);
System.out.println(time);
System.out.println(convertedDate);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment