• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

System.out.format does not have a way to print date-time easily

 
Ranch Hand
Posts: 662
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I prefer using System.out.format but I see that there is no datatype format to print date and time



Output:

now is 03/05/24



They are all broken up, unlike System.out.println which will automatically print everything.


now is
2024-03-05T10:51:22.312851400-06:00[America/Chicago]

 
Saloon Keeper
Posts: 15731
368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay, so you could do one of these:
 
Sheriff
Posts: 4646
582
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The formatting sequence %tD represents the date/time in the format YY/mm/DD.  If you want some different, then you need to build a custom formatting sequence.

For example:

%1$ refers to the first argument,  tY specifies year,  tm specifies month, etc.


 
Ron McLeod
Sheriff
Posts: 4646
582
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm sure this is available in the Javadocs somewhere, but this is a good reference for Date and Time Formatting.
 
Anil Philip
Ranch Hand
Posts: 662
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:Okay, so you could do one of these:



Hey, this trick worked! Using format() with %s


now is 2024-03-05T11:40:31.940694800-06:00[America/Chicago]

 
Anil Philip
Ranch Hand
Posts: 662
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ron McLeod wrote:
For example:



That is really hard to memorize all the flags!
Looking at Stefan's reply using printf, I tried it and found that %s also works for format().

now is 2024-03-05T11:40:31.940694800-06:00[America/Chicago]

 
Master Rancher
Posts: 5060
81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's not really a trick - this is the standard way to simply invoke toString() on whatever object you're passing.  (It also works on primitives, just printing the value.)  If the toString() is what you want, then that's all you need.  If you need something else, then they have other codes to break the data/time into parts.
 
Stephan van Hulst
Saloon Keeper
Posts: 15731
368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When using a format string, use %n for newlines, not \n.

%n is platform independent. \n is not.
 
Marshal
Posts: 79971
396
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ron McLeod wrote:I'm sure this is available in the Javadocs somewhere. . . .

Of course it is. About forty pages' worth of Javadoc, in the Formatter documentation. The other link you gave might be easier to read.
 
Anil Philip
Ranch Hand
Posts: 662
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mike Simmons wrote:It's not really a trick - this is the standard way to simply invoke toString() on whatever object you're passing..


I didn't think of it before. using the %s flag to automatically call toString().
 
Campbell Ritchie
Marshal
Posts: 79971
396
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Anil Philip wrote:. . . That is really hard to memorize all the flags! . . .

You should know better than to try memorising details of the Javadocs. [Except maybe for a cert exam.] That is why the information is there behind a hyperlink.
 
Anil Philip
Ranch Hand
Posts: 662
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:When using a format string,


I was wondering. It would be nice to be able to use a text-block as (or in) the format string for format() and also println().




But this compiles and prints



now is
2024-03-05T12:29:47.674428500-06:00[America/Chicago]



 
Saloon Keeper
Posts: 10930
87
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To get the formatting you want use DateTimeFormatter:
Output:

 
Ron McLeod
Sheriff
Posts: 4646
582
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Anil Philip wrote:I was wondering. It would be nice to be able to use a text-block as (or in) the format string for format() and also println().


It's not clear (to me) what you are wanting to do, but with a text block, I would do something like this:

 
Anil Philip
Ranch Hand
Posts: 662
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ron McLeod wrote:
It's not clear (to me) what you are wanting to do, but with a text block, I would do something like this:



I don't use jshell but when I try it here, it doesn't compile.



 
Anil Philip
Ranch Hand
Posts: 662
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Anil Philip wrote:




correction:



The textblock says it is time for lunch right now 2024-03-05T12:50:02.058123100-06:00[America/Chicago]



Thanks to all! I never knew about formatted and how to combine text-blocks with sout and soutf.
 
Mike Simmons
Master Rancher
Posts: 5060
81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Anil Philip wrote:


There's no problem with using a text block here.  The problem is that you never closed the quotes after America/Chicago, and you're missing a closing parentheses as well.

[Update - OK, I see this has now been corrected above.]
 
Anil Philip
Ranch Hand
Posts: 662
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mike Simmons wrote:The problem is that you never closed the quotes after America/Chicago, and you're missing a closing parentheses as well.


Thank you. I replied to my earlier post.
Your method is a little different than mine but yours is more in line with the traditional way and what I was trying to do.
Notice it doesn't use formatted().
 
Mike Simmons
Master Rancher
Posts: 5060
81
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Syntactically, text blocks can be used anywhere you would use a String, really.  As long as you know how to make a text block in general, there are no special tricks for combining them with specific methods like System.out.format().
 
Ron McLeod
Sheriff
Posts: 4646
582
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Anil Philip wrote:


There is no need to use System.out.format -- the formatting is performed in the String#formatted method:
 
Anil Philip
Ranch Hand
Posts: 662
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ron McLeod wrote:
There is no need to use System.out.format -- the formatting is performed in the String#formatted method:


thanks
 
Campbell Ritchie
Marshal
Posts: 79971
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ron McLeod wrote:. . . String#formatted method: . . .

I wasn't aware of that method; tt appears to be an instance counterpart of the static String#format() method, which I have known about for ages.
 
I just had the craziest dream. This tiny ad was in it.
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic