当前位置:首页 » 编程语言 » java秒

java秒

发布时间: 2022-08-10 02:29:21

java 秒换算

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class ass {
public static void main(String[] args) throws IOException {
String x;
int value;
int day;
int hour;
int minute;
int second;
int s = 60;
int t = 3600;
int d = 3600 * 24;
System.out.println("원하는 숫자를 임력하세요");
InputStreamReader ddd = new InputStreamReader(System.in);
BufferedReader buff = new BufferedReader(ddd);
x = buff.readLine();
value = Integer.parseInt(x);
// minute = value / s;
// hour = minute / t;
// second = value % s;
second = value % s;
minute = value % t / s;
hour = value % d / t;
day = value / d;

System.out.println(day + "day " + hour + "hour " + minute + "mintue "
+ second + "second");

}
}

⑵ Java 将时间转换成秒

Date类有一个getTime()可以换回秒数,例如:
public class DateToSecond
{
public static void main(String[] args)
{
Date date = new Date(System.currentTimeMillis());
System.out.println(date.getTime());
}
}

⑶ 如何用java取得年,月,日,时,分,秒

这个问题可以用两种方式得到:

方法一:在java中可以使用Date类直接获得,但是这个方法过时了,不推荐使用。

方法二:使用 java.util.Calendar 类。

代码例子:

//方法1:虽然还可以用,但是已经不建议使用,已经过时。
Datedate=newDate();
intold_y=date.getYear()+1900;//得到年份。因为得到的是1900年后至今过了多少年,所以要加1900
intold_m=date.getMonth()+1;//因为得到的结果是0~11,故而加一。
intold_d=date.getDate();//得到月份中今天的号数
System.out.println("现在是:"+old_y+"-"+old_m+"-"+old_d+"(使用过时方法)");//

//方法2:推荐使用
Calendarcalendar=Calendar.getInstance();
intnow_y=calendar.get(Calendar.YEAR);//得到年份
intnow_m=calendar.get(Calendar.MONTH)+1;//得到月份
intnow_d=calendar.get(Calendar.DATE);//得到月份中今天的号数
intnow_h=calendar.get(Calendar.HOUR_OF_DAY);//得到一天中现在的时间,24小时制
intnow_mm=calendar.get(Calendar.MINUTE);//得到分钟数
intnow_s=calendar.get(Calendar.SECOND);//得到秒数
System.out.println("现在是:"+now_y+"-"+now_m+"-"+now_d+""+now_h+":"+now_mm+":"+now_s+"(使用推荐方法)");

结果:

现在是:2015-11-9(使用过时方法)

现在是:2015-11-9 18:7:42(使用推荐方法)

⑷ java中如何取系统时间精确到秒

1 SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式 System.out.println(df.format(new Date()));// new Date()为获取当前系统时间

2 Calendar c = Calendar.getInstance();//可以对每个时间域单独修改
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int date = c.get(Calendar.DATE);
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);
int second = c.get(Calendar.SECOND);
System.out.println(year + "/" + month + "/" + date + " " +hour + ":" +minute + ":" + second);

3 Date nowTime = new Date(System.currentTimeMillis());
SimpleDateFormat
sdFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String
retStrFormatNowDate = sdFormatter.format(nowTime);

⑸ JAVA将时分秒格式的时间转化成秒数

public class TimeToSecond {

public static void main(String[] args) {

String time ="01:22:12";

String[] my =time.split(":");

int hour =Integer.parseInt(my[0]);

int min =Integer.parseInt(my[1]);

int sec =Integer.parseInt(my[2]);

int zong =hour*3600+min*60+sec;

System.out.println("共"+zong+"秒");

}

}

(5)java秒扩展阅读

java将毫秒值转换为日期时间

public static void main(String[] args) {

long milliSecond = 1551798059000L;

Date date = new Date();

date.setTime(milliSecond);

System.out.println(new SimpleDateFormat().format(date));

}

⑹ java怎样计算两个日期之间的秒数

java中Date时间可以用getTime()来获得1970年1月1日到当前时间的毫秒数,所以可以这样来计算得出两个时间的秒数:
try {

Date a = new Date();

Thread.sleep(3000);

Date b = new Date();

long interval = (b.getTime() - a.getTime())/1000;

System.out.println("两个时间相差"+interval+"秒");//会打印出相差3秒

} catch (InterruptedException e) {

e.printStackTrace();

⑺ Java 秒表

package demo;

import javax.swing.*;
import java.awt.HeadlessException;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class Timer extends JFrame {

private static final long serialVersionUID = 1L;

private static final String INITIAL_LABEL_TEXT = "00:00:00 000";

// 计数线程
private CountingThread thread = new CountingThread();

// 记录程序开始时间
private long programStart = System.currentTimeMillis();

// 程序一开始就是暂停的
private long pauseStart = programStart;

// 程序暂停的总时间
private long pauseCount = 0;

private JLabel label = new JLabel(INITIAL_LABEL_TEXT);

private JButton startPauseButton = new JButton("开始");

private JButton resetButton = new JButton("清零");

private ActionListener startPauseButtonListener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (thread.stopped) {
pauseCount += (System.currentTimeMillis() - pauseStart);
thread.stopped = false;
startPauseButton.setText("暂停");
} else {
pauseStart = System.currentTimeMillis();
thread.stopped = true;
startPauseButton.setText("继续");
}
}
};

private ActionListener resetButtonListener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
pauseStart = programStart;
pauseCount = 0;
thread.stopped = true;
label.setText(INITIAL_LABEL_TEXT);
startPauseButton.setText("开始");
}
};

public Timer(String title) throws HeadlessException {
super(title);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocation(300, 300);
setResizable(false);

setupBorder();
setupLabel();
setupButtonsPanel();

startPauseButton.addActionListener(startPauseButtonListener);
resetButton.addActionListener(resetButtonListener);

thread.start(); // 计数线程一直就运行着
}

// 为窗体面板添加边框
private void setupBorder() {
JPanel contentPane = new JPanel(new BorderLayout());
contentPane.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
this.setContentPane(contentPane);
}

// 配置按钮
private void setupButtonsPanel() {
JPanel panel = new JPanel(new FlowLayout());
panel.add(startPauseButton);
panel.add(resetButton);
add(panel, BorderLayout.SOUTH);
}

// 配置标签
private void setupLabel() {
label.setHorizontalAlignment(SwingConstants.CENTER);
label.setFont(new Font(label.getFont().getName(), label.getFont().getStyle(), 40));
this.add(label, BorderLayout.CENTER);
}

// 程序入口
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
e.printStackTrace();
}

Timer frame = new Timer("计时器");
frame.pack();
frame.setVisible(true);
}

private class CountingThread extends Thread {

public boolean stopped = true;

private CountingThread() {
setDaemon(true);
}

@Override
public void run() {
while (true) {
if (!stopped) {
long elapsed = System.currentTimeMillis() - programStart - pauseCount;
label.setText(format(elapsed));
}

try {
sleep(1); // 1毫秒更新一次显示
} catch (InterruptedException e) {
e.printStackTrace();
System.exit(1);
}
}
}

// 将毫秒数格式化
private String format(long elapsed) {
int hour, minute, second, milli;

milli = (int) (elapsed % 1000);
elapsed = elapsed / 1000;

second = (int) (elapsed % 60);
elapsed = elapsed / 60;

minute = (int) (elapsed % 60);
elapsed = elapsed / 60;

hour = (int) (elapsed % 60);

return String.format("%02d:%02d:%02d %03d", hour, minute, second, milli);
}
}
}

你可以试试,希望能帮到你!

⑻ 使用java代码实现 输入秒数获得准确时间(使用循环的方式)

importjava.text.*;
importjava.util.*;
publicclassTimeTest{
staticScannersc=newScanner(System.in);
publicstaticvoidmain(String[]args)throwsInterruptedException,ParseException{
SimpleDateFormatsdf=newSimpleDateFormat("yyyy/MM/dd/E/HH/mm/ss/SSS");
while(true){
System.out.println("输入秒数:自动转换==>毫秒");
longn=Long.parseLong(sc.nextLine().replaceAll("[^\d]+",""))*1000;
System.out.println(sdf.format(newDate(n)));
}
}
}

⑼ java 秒为多少小时,多少分,多少秒

public static Long dateDiff(String startTime, String endTime,
String format, String str) {
// 按照传入的格式生成一个simpledateformate对象
SimpleDateFormat sd = new SimpleDateFormat(format);
long nd = 1000 * 24 * 60 * 60;// 一天的毫秒数
long nh = 1000 * 60 * 60;// 一小时的毫秒数
long nm = 1000 * 60;// 一分钟的毫秒数
long ns = 1000;// 一秒钟的毫秒数
long diff;
long day = 0;
long hour = 0;
long min = 0;
long sec = 0;
// 获得两个时间的毫秒时间差异
try {
diff = sd.parse(endTime).getTime() - sd.parse(startTime).getTime();
day = diff / nd;// 计算差多少天
hour = diff % nd / nh + day * 24;// 计算差多少小时
min = diff % nd % nh / nm + day * 24 * 60;// 计算差多少分钟
sec = diff % nd % nh % nm / ns;// 计算差多少秒
// 输出结果
System.out.println("时间相差:" + day + "天" + (hour - day * 24) + "小时"
+ (min - day * 24 * 60) + "分钟" + sec + "秒。");
System.out.println("hour=" + hour + ",min=" + min);
if (str.equalsIgnoreCase("h")) {
return hour;
} else {
return min;
}

} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (str.equalsIgnoreCase("h")) {
return hour;
} else {
return min;
}
}

⑽ JAVA中如何获取毫秒和微秒数

一、获取毫秒数的代码:

微秒使用System.nanoTime()方法:如果Java程序需要高精度的计时,如1毫秒或者更小,使用System.nanoTime()方法,可以满足需求。

(10)java秒扩展阅读:

获取微秒函数System.nanoTime() 的隐患:

System.currentTimeMillis() 起始时间是基于 1970.1.1 0:00:00 这个确定的时间的,而System.nanoTime()是基于cpu核心的时钟周期来计时,它的开始时间是不确定的。

但是在多核处理器上,由于每个核心的开始时间不确定,那么

“long start = System.nanoTime();String ip = Utilities.getIpByUrl(url);long cost = System.nanoTime() - start;”

这段代码有可能会运行在两个不同的cpu核心上,从而导致得到的结果完全不符逻辑。

热点内容
如何导出薯仔缓存视频 发布:2024-04-30 14:39:36 浏览:469
图论环的算法 发布:2024-04-30 14:39:35 浏览:640
算法课项目 发布:2024-04-30 14:23:34 浏览:244
路由器无线密码从哪里看 发布:2024-04-30 13:41:07 浏览:764
安卓由哪个公司提供 发布:2024-04-30 12:27:03 浏览:417
服务器2个cpu的内存如何安装 发布:2024-04-30 12:19:02 浏览:329
如何搭建outlook服务器 发布:2024-04-30 10:46:50 浏览:638
美图忘记密码手机如何刷机 发布:2024-04-30 10:45:43 浏览:193
sql字符设置 发布:2024-04-30 10:39:03 浏览:308
androidram 发布:2024-04-30 10:36:06 浏览:281