-3

What format must the SimpleDateFormat have to format from unix timestamps to unix timestamps?

Having this code:

String format = System.getProperty("myformat");
SimpleDateFormat sdf = new SimpleDateFormat(format); 
System.out.println(sdf.format(new java.util.Date(System.currentTimeMillis())));

I can not modify the code

What must the value be of the property myformat to get a Unix timestamp?

Grim
  • 1,938
  • 10
  • 56
  • 123
  • Hm, why downvote? – Grim Apr 12 '18 at 14:53
  • 2
    I didn't downvote, but are you sure you want to use a SimpleDateFormat to implement an identity function on longs ? (or maybe you made a typo in your question) – Aaron Apr 12 '18 at 14:54
  • Possible duplicate of [Unix epoch time to Java Date object](https://stackoverflow.com/questions/535004/unix-epoch-time-to-java-date-object) – Tripp Kinetics Apr 12 '18 at 14:56
  • @Aaron Its a configuration value and I am only allowed to modify the configuration value! – Grim Apr 12 '18 at 14:56
  • Not a duplicate because I do not like to have a Java Date Object. – Grim Apr 12 '18 at 14:58
  • I don't think you actually can use a SimpleDateFormat to parse unix timestamps. You could maybe roll your own DateFormat. – Aaron Apr 12 '18 at 15:00
  • SSS only returns fractions of the current second. Possible duplicate [Get date representation in seconds? ](https://stackoverflow.com/questions/11106532/get-date-representation-in-seconds) – CannedMoose Apr 12 '18 at 15:01
  • @Aaron I dont like to parse unix timestamps. – Grim Apr 12 '18 at 15:01
  • @CannedMoose Not a duplicate, it must be SimpleDateFormat. Also seconds are not valid since the `System.currentTimeMilis()` points out that theese must be Milis! – Grim Apr 12 '18 at 15:02
  • 1
    @PeterRader I must not have understood your question then. What does "format from unix timestamps to unix timestamps" mean in your question? I assumed you had a framework forcing you to parse dates to produce timestamps and that you had one source of data that already was a timestamp but that you had to parse with SimpleDateFormat anyway, thus struggling to find a format capable both of parsing from timestamps and formatting toward timestamps. – Aaron Apr 12 '18 at 15:04
  • @Aaron I am NOT parsing anything, its only about formating. As in the code I provided, there is no `parse` there is only a `format` – Grim Apr 12 '18 at 15:08
  • Not possible using `SimpleDateFormat`, it's not what that API is for, programming model for simple date format instructs that it works with readable datetime, not with a timestamp (even though `Calendar` and `Date` are technically both). – M. Prokhorov Apr 12 '18 at 15:17
  • @PeterRader it would be good if your question didn't mention "format from unix timestamps to unix timestamps" then, but rather "display a Date formatted as unix timestamp". Anyway as I previously said and others confirmed, you can't do that with SimpleDateFormat, plain and simple. If you're ok with a DateFormat, you can totally roll your own that will do that (and probably edit your question so that someone will provide you this solution). – Aaron Apr 12 '18 at 15:22
  • This whole question sounds like a big X-Y problem to me. – M. Prokhorov Apr 12 '18 at 15:26

2 Answers2

3

Date is a wrapper for the long of the Unix timestamp.

Unfortunately SimpleDateFormat has no output for Date.getTime().

Even the new LocalDateTime with DateTimeFormatter do not provide a string representation of the long.

You could extend your own date/time formatter from SimpleDateFormat. I think 'b' is free to use. That would be a needless effort, as a use-case for a format with Unix timestamp number and other units (year, month, ...) is rare.


After comment.

Aha, a task, a puzzle. You could abuse the SimpleDateFormat symbols on an artifical derived Date.

Calendar calendar = new Calendar();
long t = calendar.getTimeInMillis(); // The Unix timestamp
int ms = (int)(t % 1000);
int yr = (int)(t / 1000);
calendar.set(Calendar.MILLIS, ms);
calendar.set(Calendar.YEAR, yr);
Date artificialDate = calendar.getTime();
String s = new SimpleDateFormat("ySSS").parse(date);

Or you could create your own Locale that instead of a month name delivers the long. That seems to be asked here. But I leave that homework to you.

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
  • Hey Joop, I am not allowed to modify the code. I can only configure the format. – Grim Apr 12 '18 at 15:25
  • Plug an agent in the JVM to modify the bytecode at runtime then? Or maybe accept that there's no trivial solution to your problem and that you need to reframe it at a higher level (do you *really* need to use your existing process that forces you to use SimpleDateFormats to format your output? Would it be possible to split the processing in two, using the process in place to produce ISO dates that will be mapped to timestamps by another process? Is an unix timestamp really the good choice of output format? etc.) – Aaron Apr 12 '18 at 15:37
  • @Aaron Never heard of the king placing impossible requests to win over the princesses hand? This seems the same kind of waste. I hope the princess is beautiful. – Joop Eggen Apr 12 '18 at 15:42
0

Unix timestamp is a calculation of seconds since Jan 01 1970 (UTC). In the other hand SimpleDateFormat is a concrete class for formatting and parsing dates. And it only gives a formatted version of the parsed Date.

If you really need to use SimpleDateFormat this is your only option.

Date date = new Date();
System.out.println(date.getTime());

SimpleDateFormat sf = new SimpleDateFormat();
sf.format(date);
System.out.println(sf.getCalendar().getTimeInMillis());

OUTPUT:

1523545871501
1523545871501
Roshana Pitigala
  • 8,437
  • 8
  • 49
  • 80