Home > OS >  What is the purpose of %s, %tc, %Tc?
What is the purpose of %s, %tc, %Tc?

Time:01-26

I was studying a program about getting dates, time and then after browsing online I see this one:

System.out.printf("The date is %s\n", new Date() );

System.out.printf("The date is %tc\n", new Date() );

System.out.printf("The DATE is %Tc\n", new Date() );

which seems to be straightforward but I want to understand the use of %s, %tc, %Tc here.

CodePudding user response:

it's for formatting you might check https://www.baeldung.com/java-printstream-printf

CodePudding user response:

These are special characters that are being used to format a date. Here is a good example for this: http://www.java2s.com/Tutorial/Java/0120__Development/UnixdateformattcTc.htm

import java.util.Date;

public class MainClass {

  public static void main(String[] args) {
    Date now = new Date();
    System.out.printf("Unix date format: %tc/%Tc\n", now, now);
  }
}

And the result:

Unix date format: Thu May 24 15:58:29 PDT 2007/THU MAY 24 15:58:29 PDT 2007

All credits to this page

  •  Tags:  
  • Related