import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Timer;
import java.util.TimerTask;
public class timer {
public static void main(String[] args) {
Thread01 th01 = new Thread01();
Timer timeStart = new Timer();
timeStart.schedule(th01, 1000, 1000);
}
}
class Thread01 extends TimerTask {
public void run() {
Calendar cal = new GregorianCalendar();
int y, m, d, h, m1, s;
y = cal.get(Calendar.YEAR);
m = cal.get(Calendar.MONTH)+1;
d = cal.get(Calendar.DATE);
h = cal.get(Calendar.HOUR);
m1 = cal.get(Calendar.MINUTE);
s = cal.get(Calendar.SECOND);
int[] timeArr1 = {y, m, d, h, m1, s};
String[] timeArr2 = {"년 ", "월 ", "일 ", "시 ", "분 ", "초"};
for (int i=0; i<timeArr1.length; i++) {
System.out.print(timeArr1[i] + timeArr2[i]);
}
System.out.println();
}
}
import java.util.Calendar;
import java.util.Timer;
import java.util.TimerTask;
public class timer {
public static void main(String[] args) {
Timer t = new Timer();
Tictoc tt = new Tictoc();
tt.schedule(tt, 1000, 1000);
}
}
class Tictoc extends TimerTask {
public void run() {
printDays();
}
private void printDays() {
Calendar cal = Calendar.getInstance();
System.out.print(cal.get(Calendar.YEAR)+"년 ");
System.out.print(cal.get(Calendar.MONTH)+"월 ");
System.out.print(cal.get(Calendar.DATE)+"일 ");
System.out.print(cal.get(Calendar.HOUR)+"시 ");
System.out.print(cal.get(Calendar.MINUTE)+"분 ");
System.out.println(cal.get(Calendar.SECOND)+"초");
}
}
AWT 패키지를 이용하여 새 창으로 띄우기 (JFrame, JLabel, Thread, LocalDateTime)
import java.awt.*;
import java.time.*;
import javax.swing.*;
class TimeNow implements Runnable {
private JLabel laTime;
public TimeNow (JLabel laTime) {
this.laTime = laTime;
}
@Override
public void run () {
while (true) {
LocalDateTime now = LocalDateTime.now();
int year = now.getYear();
int month = now.getMonthValue();
int day = now.getDayOfMonth();
int hour = now.getHour();
int minute = now.getMinute();
int second = now.getSecond();
int[] nowArr1 = {year, month, day, hour, minute, second};
String[] nowArr2 = {".", ".", " ", ":", ":", ""};
String timeText = "";
for (int i=0; i<nowArr1.length; i++) {
timeText += Integer.toString(nowArr1[i])+nowArr2[i];
}
laTime.setText(timeText);
}
}
} // TimeNow class end
public class goHome extends JFrame {
private Container c;
private JLabel laTime;
private void initObject() {
c = getContentPane();
laTime = new JLabel(""); // 타이머 값이 출력될 레이블
}
private void initSetting() {
c.setLayout(new FlowLayout());
laTime.setFont(new Font("Century Schoolbook", Font.BOLD, 80));
setTitle("정각 알리미");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(900,160);
Toolkit tk = Toolkit.getDefaultToolkit();
Dimension screenSize = tk.getScreenSize();
setLocation(screenSize.width / 4*3-300, screenSize.height / 4*3);
}
private void initBatch() {
c.add(laTime);
}
private void getTime() {
Thread t1 = new Thread(new TimeNow(laTime));
t1.start();
}
public goHome() {
initObject(); // 객체 생성
initSetting(); // 객체 속성 변경
initBatch(); // 객체 배치
getTime(); // 현재 시간 정보를 받아오는 스레드 실행
setVisible(true);
}
public static void main(String[] args) {
new goHome();
}
}
참고 :
https://blog.naver.com/chmh0805/222154216588
https://blog.naver.com/jiae7634/222837488965
'개발일지' 카테고리의 다른 글
Window > Preferences > Code Style > Code > Method body > edit (0) | 2023.02.20 |
---|---|
이클립스에서 제공하는 Tomcat 목록보다 상위 버전을 연동해야 할 때 (0) | 2023.02.13 |
java.text.NumberFormat 클래스로 숫자에 형식 지정하기 (콤마, 화폐, 퍼센트) (0) | 2023.02.09 |