java 将“60.0” 转int
时间: 2023-10-04 15:07:09 浏览: 33
如果您要将字符串 "60.0" 转换为整数,则需要先将其转换为浮点数,然后再将浮点数转换为整数。您可以使用以下代码将字符串转换为整数:
```
String str = "60.0";
double numDouble = Double.parseDouble(str);
int numInt = (int) numDouble;
```
在这个例子中,首先使用Double.parseDouble()方法将字符串 "60.0" 转换为双精度浮点数类型的 numDouble,然后使用强制类型转换将其转换为整数类型的 numInt。
需要注意的是,将浮点数转换为整数会将小数部分舍去,因此在本例中,numInt 的值将为 60。
相关问题
Java转Double类型经纬度为度分秒格式
可以使用以下代码将Java中的经纬度转换为度分秒格式:
```java
public static String convertToDMS(double coordinate) {
int degrees = (int) coordinate;
int minutes = (int) ((coordinate - degrees) * 60);
double seconds = (coordinate - degrees - minutes / 60.0) * 3600;
return degrees + "°" + minutes + "'" + String.format("%.2f", seconds) + "\"";
}
```
使用示例:
```java
double longitude = 116.4074;
double latitude = 39.9042;
String dmsLongitude = convertToDMS(longitude); // 116°24'26.64"
String dmsLatitude = convertToDMS(latitude); // 39°54'15.12"
```
其中,`convertToDMS`方法将输入的经度或纬度转换为度分秒格式的字符串。首先将度数部分取整作为度数,然后将小数部分乘以60取整作为分钟数,最后将剩余的小数部分乘以3600得到秒数。最后将度、分、秒按照格式拼接成字符串返回。
抖音时间罗盘java代码
抖音时间罗盘是一种根据系统时间动态变换背景颜色的效果,可以通过以下Java代码实现:
```java
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
public class DouyinTimeCompass extends JPanel implements ActionListener {
private Timer timer;
public DouyinTimeCompass() {
timer = new Timer(1000, this);
timer.start();
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
Calendar cal = Calendar.getInstance();
int hour = cal.get(Calendar.HOUR_OF_DAY);
int minute = cal.get(Calendar.MINUTE);
int second = cal.get(Calendar.SECOND);
int r = 200;
int x = getWidth() / 2;
int y = getHeight() / 2;
RenderingHints rh = new RenderingHints(
RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setRenderingHints(rh);
drawBackground(g2d, x, y, r);
drawHourHand(g2d, x, y, r, hour, minute);
drawMinuteHand(g2d, x, y, r, minute, second);
drawSecondHand(g2d, x, y, r, second);
}
private void drawBackground(Graphics2D g2d, int x, int y, int r) {
int[] colors = {0xFFEEC900, 0xFF4285F4, 0xFF34A853, 0xFFEA4335};
int direction = Calendar.getInstance().get(Calendar.SECOND) % 4;
g2d.setColor(new Color(colors[direction]));
g2d.fillOval(x - r, y - r, 2 * r, 2 * r);
}
private void drawHourHand(Graphics2D g2d, int x, int y, int r, int hour, int minute) {
int handRadius = (int) (r * 0.4);
int angle = (int) ((hour + minute / 60.0) / 12 * 360 - 90);
int handX = (int) (x + Math.cos(Math.toRadians(angle)) * handRadius);
int handY = (int) (y + Math.sin(Math.toRadians(angle)) * handRadius);
g2d.setColor(Color.white);
g2d.drawLine(x, y, handX, handY);
}
private void drawMinuteHand(Graphics2D g2d, int x, int y, int r, int minute, int second) {
int handRadius = (int) (r * 0.6);
int angle = (int) ((minute + second / 60.0) / 60 * 360 - 90);
int handX = (int) (x + Math.cos(Math.toRadians(angle)) * handRadius);
int handY = (int) (y + Math.sin(Math.toRadians(angle)) * handRadius);
g2d.setColor(Color.white);
g2d.drawLine(x, y, handX, handY);
}
private void drawSecondHand(Graphics2D g2d, int x, int y, int r, int second) {
int handRadius = (int) (r * 0.9);
int angle = (int) ((second) / 60.0 * 360 - 90);
int handX = (int) (x + Math.cos(Math.toRadians(angle)) * handRadius);
int handY = (int) (y + Math.sin(Math.toRadians(angle)) * handRadius);
g2d.setColor(Color.white);
g2d.drawLine(x, y, handX, handY);
}
public void actionPerformed(ActionEvent e) {
repaint();
}
public static void main(String[] args) {
JFrame frame = new JFrame("Douyin Time Compass");
DouyinTimeCompass compass = new DouyinTimeCompass();
frame.add(compass);
frame.setSize(new Dimension(400, 400));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
```
以上代码是通过Java Swing来实现抖音时间罗盘的效果。代码中使用了Timer类来定时更新界面,根据系统时间获取小时、分钟、秒钟,然后根据这些值计算时针、分针和秒针的位置,并在界面上绘制各个指针。同时,根据当前秒钟数的奇偶性来决定背景颜色的变换。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pptx](https://img-home.csdnimg.cn/images/20210720083543.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)