Example usage for java.lang String format

List of usage examples for java.lang String format

Introduction

In this page you can find the example usage for java.lang String format.

Prototype

public static String format(Locale l, String format, Object... args) 

Source Link

Document

Returns a formatted string using the specified locale, format string, and arguments.

Usage

From source file:Main.java

public static void main(String args[]) {

    System.out.println(String.format("Hi %s at %s", "name", "your place"));
}

From source file:MainClass.java

public static void main(String[] a) {
    double x = 27.5, y = 33.75;
    String outString = String.format("x = %15.2f y = %14.3g", x, y);

    System.out.println(outString);
}

From source file:Main.java

public static void main(String[] args) {
    LocalDate today = LocalDate.now();

    LocalDate nextYear = today.plusYears(1);

    System.out.println(String.format("Today %s and next year %s", today, nextYear));
}

From source file:Main.java

public static void main(String[] args) {
    Instant t1 = Instant.now();
    long hours = 2;
    long minutes = 30;
    Instant t2 = t1.plus(hours, ChronoUnit.HOURS).plus(minutes, ChronoUnit.MINUTES);

    System.out.println(String.format("now %s and later %s", t1, t2));
}

From source file:Main.java

public static void main(String[] args) {

    DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss'Z'");

    LocalDateTime now = LocalDateTime.now();
    LocalDateTime then = now.minusDays(7);

    System.out.println(String.format("Now:  %s\nThen: %s", now.format(format), then.format(format)));

}

From source file:Main.java

public static void main(String[] args) {
    ScriptEngineManager mgr = new ScriptEngineManager();
    List<ScriptEngineFactory> factories = mgr.getEngineFactories();
    for (ScriptEngineFactory factory : factories) {
        System.out.println(String.format("engineName: %s, THREADING: %s", factory.getEngineName(),
                factory.getParameter("THREADING")));
    }//from w ww  .j  ava2  s.  co m
}

From source file:Main.java

public static void main(String[] args) {
    LocalTime now = LocalTime.now();
    LocalTime currentTimeInLosAngeles = LocalTime.now(ZoneId.of("America/Los_Angeles"));
    System.out.println(String.format("now is %s and in LA is %s", now, currentTimeInLosAngeles));

    ZoneId leavingZone = ZoneId.of("Asia/Tel_Aviv");
    ZoneId arrivingZone = ZoneId.of("America/New_York");

    LocalDateTime leaving = LocalDateTime.of(2014, Month.JULY, 16, 23, 00);
    ZonedDateTime departure = ZonedDateTime.of(leaving, leavingZone);

    ZonedDateTime arrival = departure.withZoneSameInstant(arrivingZone).plusHours(11).plusMinutes(51);

    DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy-MM-d  HH:mm");
    System.out.println(String.format("Departure: %s", departure.format(format)));
    System.out.println(String.format("Arrival: %s", arrival.format(format)));
}

From source file:Main.java

public static void main(String[] args) {
    LocalDate date = LocalDate.of(2014, 2, 15); // 2014-02-15
    Month february = date.getMonth();
    System.out.println(february); // FEBRUARY

    int februaryIntValue = february.getValue();
    System.out.println(februaryIntValue); // 2

    // 28 , 29// w  w w. j  ava  2  s  .c  om
    System.out.println(String.format("%s , %s", february.minLength(), february.maxLength()));

    Month firstMonthOfQuarter = february.firstMonthOfQuarter();
    System.out.println(firstMonthOfQuarter); // JANUARY
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    InetAddress ip = InetAddress.getLocalHost();
    System.out.println("Current IP address : " + ip.getHostAddress());

    NetworkInterface network = NetworkInterface.getByInetAddress(ip);
    byte[] mac = network.getHardwareAddress();
    System.out.print("Current MAC address : ");

    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < mac.length; i++) {
        sb.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : ""));
    }//w  ww. j a  v  a2  s.c  om
    System.out.println(sb.toString());
}

From source file:Main.java

public static void main(final String[] args) {
    ConcurrentHashMap<Integer, UUID> concurrentHashMap = new ConcurrentHashMap<>();

    for (int i = 0; i < 100; i++) {
        concurrentHashMap.put(i, UUID.randomUUID());
    }//from www.ja  v  a  2 s .  c o  m

    int threshold = 1;

    concurrentHashMap.forEachValue(threshold, System.out::println);

    concurrentHashMap.forEach((id, uuid) -> {
        if (id % 10 == 0) {
            System.out.println(String.format("%s: %s", id, uuid));
        }
    });

    String searchResult = concurrentHashMap.search(threshold, (id, uuid) -> {
        if (String.valueOf(uuid).contains(String.valueOf(id))) {
            return new String(id + ":" + uuid);
        }
        return null;
    });

    System.out.println(searchResult);
}