【JAVA】currentTimeMillis显示当前时间,科学计数法和强制转换

自己的状况

目前JAVA 才学到基本程序设计,看着老师的直播录像加自己pdf(小声BB,pdf看久了真的眼睛酸)。由于才转到软工,还剩4大节课没补上。需要加快速度了。
由于在做其他的作业,今天花费的时间不是很多。

今天的学习情况

大部分看的和c语言中的类似,就不一一详述
主要讲讲自己学到的新东西
(有图片的上传太大了,调不到就略了)

科学计数法:

例如1.23456*102

== 1.23456E2 或者1.23456E+2 (E 或者e表示指数)

显示时间的小程序

通过调用System.currentTimeMillis()返回当前时间。
(该方法是返回从GMT1970年1月1日0时到现在当前的毫秒数,返回的是0区的时间,所以不是中国时间= =
代码基本上和书上差不多)

public static void main(String[] args){
		
		/*先获取时间(获取的为毫秒milliseconds)
		 * milliseconds / 1000  obtain the totalseconds
		 * totalseconds % 60 obtain current seconds in the minute in the hour
		 * totalseconds / 60 obtain totalminute
		 * totalminute % 60 obtain the currten minute in the hour
		 * totalminute / 60 obtain the totalhour
		 * totalhour %24 obtain the currten hour
		 * */
		//obtain the total milliseconds since midnight ,jan 1, 1970
		long totalMilliseconds = System.currentTimeMillis();//定义一个长整型    =  GMT时间
		
		//Obtain the total seconds since midnight, Jan 1, 1970
		long totalSeconds = totalMilliseconds / 1000;
		
		//Computer the current second in the minute in the hour
		long currentSeconds = totalSeconds % 60;
		
		//Obetain the total minutes
		long totalMinutes = totalSeconds / 60;
		
		//obtain the currentMinutes
		long currentMinutes = totalMinutes % 60;
		
		//obtain the totalhours
		long totalHours = totalMinutes / 60;
		
		//obtain the currentHours
		long currentHours = totalHours % 24;
		
		//display results
		System.out.println("Current time is:" + currentHours + ":" + currentMinutes + ":" + currentSeconds );
		System.out.println(totalMilliseconds);

显示的是0时区的时间

中国在东8区。

强制转换

在Java中

short i = 2;
i = i + 1//这是错误的,会报错
System.out.println(i);

上面因为i是short ,下面i = i + 1;中,i默认的是int类型
所以改为i =(int) (i+1);就是正确
但是i += 1;这种写法就不会报错

short i = 2;
i *= 0.1;
System.out.println(i);

这里i输出的是0
因为i =(int) (i * 0.1)

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值