1. 程式人生 > 程式設計 >java long轉String +Codeforces110A案例

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

一個小栗子

獲取當前時間和半個小時之前的時間

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案例就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支援我們。