Skip to content

Instantly share code, notes, and snippets.

@alibehzadian
Last active August 23, 2024 18:26
Show Gist options
  • Save alibehzadian/45f9d706ca6e671049b5c5ba67268697 to your computer and use it in GitHub Desktop.
Save alibehzadian/45f9d706ca6e671049b5c5ba67268697 to your computer and use it in GitHub Desktop.
Examples of console output formats in Java
public class Main {
public static void main(String[] args) {
System.out.print(1);
System.out.print(1.5f);
System.out.print(1.5);
System.out.print('a');
System.out.print("Hello!");
System.out.println();
System.out.println(1);
System.out.println(1.5f);
System.out.println(1.5);
System.out.println('a');
System.out.println("Hello!");
System.out.print("Hello! \"Master Developer!\"");
// %[flags][width][.precision]conversion-character
System.out.printf("Hello World!");
System.out.printf("Hello %n World! %n");
System.out.printf("Hello %s %n", "Ali!");
System.out.printf("Hello %s\n", "Ali!");
System.out.printf("'%30s' %n", "Master Developer!");
System.out.printf("'%-30s' %n", "Master Developer!");
System.out.printf("'%10.6s' %n", "Master Developer!");
System.out.printf("%s %s %s %s %n", "Welcome ", "to ", "Master ", "Developer!");
System.out.printf("%1$s %2$s %3$s %4$s %n", "Welcome ", "to ", "Master ", "Developer!");
System.out.printf("%4$s %3$s %2$s %1$s %n", "Developer!", "Master ", "to ", "Welcome ");
System.out.printf("Number is: %d %n", 1);
System.out.printf("Number is: %d %n", 10000);
System.out.printf("Number is: %,d %n", 10000);
System.out.printf(Locale.US, "Number is: %,d %n", 10000);
System.out.printf(Locale.GERMANY, "Number is: %,d %n", 10000);
System.out.printf("Number is: %f %n", 1.5f);
System.out.printf("Number is: %f %n", 1.5);
System.out.printf("Number is: '%.2f' %n", 5.1473);
System.out.printf("Number is: '%5.2f' %n", 5.1473);
System.out.printf("Number is: '%.2e' %n", 156.789);
System.out.printf("Boolean true: %b %n", true);
System.out.printf("Boolean false: %b %n", false);
System.out.printf("Boolean true: %B %n", true);
System.out.printf("Boolean false: %B %n", false);
System.out.printf("Character %c %n", 'a');
System.out.printf("Character %C %n", 'a');
// H: hours
// M: minutes
// S: seconds
// L: milliseconds
// N: nanoseconds
// p: a.m./p.m.
// z: time-zone offset
Date date = new Date();
System.out.printf("%tT %n", date);
System.out.printf("%tH %n", date);
System.out.printf("%tM %n", date);
System.out.printf("%tS %n", date);
System.out.printf("%tL %n", date);
System.out.printf("%tN %n", date);
System.out.printf("%tp %n", date);
System.out.printf("%tz %n", date);
System.out.printf("%1$tH:%1$tM:%1$tS %1$tp %1$tL %1$tN %1$tz %n", date);
// A: full day of the week
// d: two-digit day of the month
// B: full month name
// m: two-digit month
// Y: year in four digits
// y: two digits of the year
System.out.printf("%tA %n", date);
System.out.printf("%td %n", date);
System.out.printf("%tB %n", date);
System.out.printf("%tm %n", date);
System.out.printf("%tY %n", date);
System.out.printf("%ty %n", date);
System.out.printf("%1$tA, %1$tB %1$tY %n", date);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment