您的位置:首页技术文章
文章详情页

java long转String +Codeforces110A案例

【字号: 日期:2022-08-24 09:25:32浏览:5作者:猪猪

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; }

一个小栗子

获取当前时间和半个小时之前的时间

java long转String +Codeforces110A案例

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案例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持好吧啦网。

标签: Java
相关文章: