Guten Abend, Eine Frage: Wie kann ich mir die Zeit von der CPU also die Zeit vom PC holen? Bei C++ gab es das Befehl gettime So haben wir es gemacht: struct time t; //Struktur vom Typ time gettime (&t); sec=t.ti_sec; min=t.ti_min; stunde=t.ti_hour; wie funktioniert das bei JAVA? Danke
long ttime = System.currentTimeMillis(); oder Calendar c = Calendar.getInstance(); und dann sowas wie stunde = c.get(Calendar.HOUR);
Je nachdem, was du machen willst:
1 | Date now = new Date(); |
Aktueller ist allerdings Calendar:
1 | Calendar rightNow = Calendar.getInstance(); |
http://download.oracle.com/javase/1.5.0/docs/api/java/util/Calendar.html
Hans Mayer schrieb: > stunde = c.get(Calendar.HOUR); Vielleicht statt HOUR->HOUR_OF-DAY HOUR=11//geht bis 12 HOUR_OF_DAY=23//geht bis 24 Aber danke hat mir viel geholfen!
Eins noch: Ich habs jetzt mit Stunde Minute uns sekunde gemacht und jetzt möchte ich daraus eine UHR machen: Calendar c = Calendar.getInstance(); stunde = c.get(Calendar.HOUR_OF_DAY); minute= c.get(Calendar.MINUTE); sekunde= c.get(Calendar.SECOND); jLabel2.setText(String.valueOf(stunde)); jLabel3.setText(String.valueOf(minute)); jLabel4.setText(String.valueOf(sekunde)); alpha=stunde*30; y= (int) ((int) r * Math.cos(1.0 alpha Math.PI / (180 * 1.0))); x= (int) ((int) r * Math.sin(1.0 alpha Math.PI / (180 * 1.0))); Wie kann ich daraus eine Uhr machen? Also wie mach ich eine Linie die der Zeit folgt? Danke
1 | package test; |
2 | |
3 | import java.awt.Color; |
4 | import java.awt.Graphics; |
5 | import java.awt.Graphics2D; |
6 | import java.awt.Rectangle; |
7 | import java.awt.event.ActionEvent; |
8 | import java.awt.event.ActionListener; |
9 | import java.awt.geom.Ellipse2D; |
10 | import java.awt.geom.Line2D; |
11 | |
12 | import javax.swing.JComponent; |
13 | import javax.swing.JFrame; |
14 | import javax.swing.Timer; |
15 | |
16 | public class Clock extends JComponent { |
17 | |
18 | private static final long serialVersionUID = 4229018579689301767L; |
19 | |
20 | public Clock() { |
21 | new Timer(1000, new ActionListener() { |
22 | |
23 | @Override
|
24 | public void actionPerformed(ActionEvent e) { |
25 | repaint(); |
26 | }
|
27 | }).start(); |
28 | }
|
29 | |
30 | @Override
|
31 | protected void paintComponent(Graphics g) { |
32 | Graphics2D g2 = (Graphics2D) g.create(); |
33 | try { |
34 | Rectangle visRect = getVisibleRect(); |
35 | g2.setColor(super.getBackground()); |
36 | g2.fill(visRect); |
37 | paintClock(g2, visRect.width, visRect.height); |
38 | } finally { |
39 | g2.dispose(); |
40 | }
|
41 | }
|
42 | |
43 | private void paintClock(Graphics2D g, int width, int height) { |
44 | System.out.println("Paint clock (w = " + width + ", h = " + height + ")"); |
45 | g.setColor(Color.BLACK); |
46 | //Paint the clock...
|
47 | g.draw(new Ellipse2D.Double(0, 0, width, height)); |
48 | g.setColor(Color.RED); |
49 | g.draw(new Line2D.Double(0, 0, width, height)); |
50 | }
|
51 | |
52 | public static void main(String[] args) { |
53 | JFrame frame = new JFrame("clocktest"); |
54 | frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
55 | frame.setSize(400, 400); |
56 | frame.add(new Clock()); |
57 | frame.setLocationRelativeTo(null); |
58 | frame.setVisible(true); |
59 | }
|
60 | }
|
die paintClock Methode müsstest du jetzt aber selber füllen...
Bitte melde dich an um einen Beitrag zu schreiben. Anmeldung ist kostenlos und dauert nur eine Minute.
Bestehender Account
Schon ein Account bei Google/GoogleMail? Keine Anmeldung erforderlich!
Mit Google-Account einloggen
Mit Google-Account einloggen
Noch kein Account? Hier anmelden.