java long转String +Codeforces110A案例
long转String常用的两种方法:
long n=scanner.nextLong(); String s=Long.toString(n);//第一种方法 String s=String.valueOf(n);//第二种方法
代码实例(codeforces 110A):
import java.util.Scanner; public class Main { public static void main(String[] args) { // write your code here Scanner scanner=new Scanner(System.in); long n=scanner.nextLong(); //String s=Long.toString(n);//第一种方法 String s=String.valueOf(n);//第二种方法 int count=0; for(int i=0;i<s.length();i++){ if(s.charAt(i)=='4'||s.charAt(i)=='7'){ count++; } } if(count==4||count==7){ System.out.println("YES"); }else { System.out.println("NO"); } } }
补充知识:java string类型和long类型之间的转换以及获取当前时间
1、获取当前的时间
//获取当前的时间 public static String get(){ Date d=new Date(); SimpleDateFormat sim=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); String time=sim.format(d); System.out.println(time); return time; }
2、把字符串类型的时间转换为long类型
public static long pare(String time){ SimpleDateFormat sim=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); long s=0; try { s=sim.parse(time).getTime(); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } return s; }
3、把long类型的时间变成String类型
public static String topare(long l){ Date date = new Date(l); SimpleDateFormat sim=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); String time=sim.format(date); return time; }
一个小栗子
获取当前时间和半个小时之前的时间
package cn.com.tools; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class GetTime { //获取当前的时间 public static String get(){ Date d=new Date(); SimpleDateFormat sim=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); String time=sim.format(d); System.out.println(time); return time; } //获取半个小时之前的时间 public static void main(String[] args) { String t1=get(); long t0=pare(t1); long t2=t0-1800000; //把long类型转换成string类型 String tt=topare(t2); System.out.println(tt); } //把字符串类型的时间转换为long类型 public static long pare(String time){ SimpleDateFormat sim=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); long s=0; try { s=sim.parse(time).getTime(); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } return s; } //把long类型的时间变成String类型 public static String topare(long l){ Date date = new Date(l); SimpleDateFormat sim=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); String time=sim.format(date); return time; } }
以上这篇java long转String +Codeforces110A案例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。
相关文章
MyBatis-Plus标签@TableField之fill自动填充方式
这篇文章主要介绍了MyBatis-Plus标签@TableField之fill自动填充方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教2022-06-06springboot如何配置上传文件的maxRequestSize
这篇文章主要介绍了springboot如何配置上传文件的maxRequestSize,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教2022-03-03Java中通过ZipOutputStream类如何将多个文件打成zip
ZipOutputStream 是Java中用于创建ZIP文件的类,它是 java.util.zip 包中的一部分,通过使用 ZipOutputStream ,可以将多个文件压缩到一个ZIP文件中,这篇文章主要介绍了Java中(ZipOutputStream)如何将多个文件打成zip,需要的朋友可以参考下2023-09-09浅谈spring中isolation和propagation的用法
这篇文章主要介绍了浅谈spring中isolation 和propagation的用法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧2020-07-07
最新评论