How to use string format with calendar in Java

5 Answers

0 votes
import java.util.Calendar;

public class MyClass {
    public static void main(String args[]) {
        Calendar cal = Calendar.getInstance();
        cal.set(2020, 0, 12);

        String s = String.format("%1$tB / %1$te / %1$tY", cal);
        System.out.println(s);
    }
}




/*
run:

January / 12 / 2020

*/

 



answered Oct 9, 2020 by avibootz
0 votes
import java.util.Calendar;

public class MyClass {
    public static void main(String args[]) {
        Calendar cal = Calendar.getInstance();
        cal.set(2020, 0, 12);

        String s = String.format("%1$tA %1$ta %1$td", cal);
        System.out.println(s);
    }
}




/*
run:

Sunday Sun 12

*/

 



answered Oct 9, 2020 by avibootz
0 votes
import java.util.Calendar;

public class MyClass {
    public static void main(String args[]) {
        Calendar cal = Calendar.getInstance();
        cal.set(2020, 0, 12);

        String s = String.format("%1$tY %1$ty", cal);
        System.out.println(s);
    }
}




/*
run:

2020 20

*/

 



answered Oct 9, 2020 by avibootz
0 votes
import java.util.Calendar;

public class MyClass {
    public static void main(String args[]) {
        Calendar cal = Calendar.getInstance();
        cal.set(2020, 0, 12);

        String s = String.format("%1$tB %1$tb", cal);
        System.out.println(s);
    }
}




/*
run:

January Jan

*/

 



answered Oct 9, 2020 by avibootz
0 votes
import java.util.Calendar;
import java.time.Instant;
import java.util.Date;

public class MyClass {
    public static void main(String args[]) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(Date.from(Instant.now()));

        String s = String.format("%1$tH (%1$tI) : %1$tM : %1$tS %1$tp", cal);
        System.out.println(s);
    }
}




/*
run:

18 (06) : 39 : 41 pm

*/

 



answered Oct 9, 2020 by avibootz

Related questions

1 answer 186 views
1 answer 206 views
1 answer 239 views
1 answer 199 views
1 answer 218 views
1 answer 203 views
...