scala时间处理及其他代码块

1.时间戳(毫秒)转时间

    var time2 = 1541827261631d;
    var result2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(time2)
    println(result2)

普通显示的

//long timeStamp = 1495777335060;//直接是时间戳
long timeStamp = System.currentTimeMillis();  //获取当前时间戳,也可以是你自已给的一个随机的或是别人给你的时间戳(一定是long型的数据)
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//这个是你要转成后的时间的格式
String sd = sdf.format(new Date(timeStamp));   // 时间戳转换成时间
        System.out.println(sd);//打印出你要的时间

2.保留小数点后12位数字

 // 数据格式 经纬度小数度只保存12个。如果后面有0舍弃
    def dataformat(nn: Int) : String ={
      var df2 = new DecimalFormat("###.000000000000");//定义格式
      return df2.format((nn.toDouble/60000).toFloat) //将值再转位float这个的好处是,例如:原来的:3.4567000000,转为3.4567
    }

3.2进制转10进制

    //2进制转10进制
    @Test
    public void tt(){
        int m=Integer.parseInt("1001",2);
     System.out.println(m);
    }

4.16进制转2进制

    // 16进制转2进制
    @Test
    public void function1( ) {
        int n = Integer.parseInt("3", 16);
        String result = Integer.toBinaryString(n);
        //预先定义一个8位0
        StringBuilder zero =new StringBuilder("00000000");
        String result3 = zero.substring(0, zero.length() - result.length()) + result;
        // int r = Integer.parseInt(result);
        // System.out.println(r);
        System.out.println(result3);
    }

5.进制转换

@Test
    public void test07() {
        int i = Integer.parseInt("3", 16); // 16代表前的的是十六进制数值 7-8位
        String s = Integer.toHexString(i); // 需要时再转回字符串形式
        String s3 = Integer.toHexString(100000); // 需要时再转回字符串形式
        System.out.println(i + "--i");// true
        System.out.println(s3 + ":s3---");// true 3e7--->999 64---100
        System.out.println(s + "+++s");// trues

    }

6.正则匹配

// 正则表达式匹配
    // 参考 :https://zhidao.baidu.com/question/552412036.html
    // 参考 :https://www.cnblogs.com/sparkbj/articles/6207103.html
    @Test
    public void test06() { // [0-9]{1,3} 以数字长度为1到3位 。以a字符开头以b字符结尾
        Pattern p = Pattern.compile("a[0-9]{1,3}b");
        Matcher m = p.matcher("a724b");
        boolean b = m.matches();
        System.out.println(b);// true
    }

 7.截取特殊符号前后字符串

//java版:获取特殊符号后面的字符串
String  str ="abc.rar";
String [] strs = str.split("[.]");
System.out.println("符号前面的字符串="+strs[0]);
System.out.println("符号后面的字符串="+strs[1]);

//scala版
    var  str ="abc.rar";
    var strs = str.split("[.]");
    println("符号前面的字符串="+strs.apply(0))
    println("符号后面的字符串="+strs.apply(1))

//scala版推荐使用
    val a = "aa-bc-xx"
    val i = a.indexOf("-")
    val x = a.indexOf("-", i)
    val one = a.substring(0, i)
    print(one) //aa
    val two = a.substring(i + 1, x)
    print(two) //bc
    val three = a.substring(x + 1)
    print(three)//xx

 8.时间格式化

//scala版    
    var time2 = "2018-11-13 15:49:4"
    var date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(time2);
    println(date)
    var now = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
    println(now)

//java版 先用parse将字符串解析成date型,再用format格式化成string。
  Date date = new SimpleDateFormat("yyyy-MM-dd").parse("2005-06-09");
  String now = new SimpleDateFormat("yyyy年MM月dd日").format(date);

 综合,下面有可能会重复,但实现方式是不一样的

 //16 转 10
  def SixteenTurnTen (SixteenString:String): String ={
    var str = new BigInteger(SixteenString,16).toString(10)
    str
  }
//16 转 2 单参数
def SixteenTurnTwo (SixteenString:String): String ={
var str = new BigInteger(SixteenString,16).toString(2)
var a = str.length;
while (a < 8){
str = "0"+str
a += 1
}
str
}
//16 转 2
  def SixteenTurnTwo (SixteenString:String,lengthStr:Int): String ={
    var str = new BigInteger(SixteenString,16).toString(2)
    var a = str.length;
    while (a < lengthStr){
      str = "0"+str
      a += 1
    }
    str
  }
  //2 转 10
  def TwoTurnTen (SixteenString:String): String ={
    var str = new BigInteger(SixteenString,2).toString(10)
    str
  }
  //经纬度计算并且四舍五入
  def Rounding(SixteenDouble:Double):String = {
    var formatDouble = NumberFormat.getNumberInstance()
    formatDouble.setMaximumFractionDigits(12)
    formatDouble.setRoundingMode(RoundingMode.UP)
    formatDouble.format(SixteenDouble/60000)
  }
  //时间戳转化为时间
  def tranTimeToString(tm:String) :String={
    val fm = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
    val tim = fm.format(new Date(tm.toLong))
    tim
  }

  //补齐0
  def Complement(str:String,length:Int): String = {
    var stringValue = str
    var value = str.length
    if (value < length){
      while (value < length){
        stringValue = "0" + str
        value = length
      }
    }
    stringValue
  }

 9.一个程序同时消费多个kafkak里的数据

import org.apache.spark.streaming.kafka010.ConsumerStrategies
//下面是spark streaming消费多个主题
  val topic = Array("k1", "k2","k3")
 //接收数据
    var stream: InputDStream[ConsumerRecord[String, String]] = KafkaUtils.createDirectStream[String, String](
      ssc,
      PreferConsistent,
      ConsumerStrategies.Subscribe[String, String](topic, kafkaParams)  // ---
    )
    var events: DStream[String] = stream.map(r => r.value()+"_"+r.topic())

//下面是spark structed streaming消费多个主题
val kafkaKvPair = spark
  .read
  .format("kafka")
  .option("kafka.bootstrap.servers", "host1:port1,host2:port2")
  .option("subscribe", "topic1")
  .load()
  .selectExpr("CAST(key AS STRING)", "CAST(value AS STRING)")
  .as[(String, String)] 

多个topic就逗号分割一下即可,比如
.option("subscribe", "topic1,topic2,topic3"

 10.两个map合并为一个map且根据主键合并

def main(args: Array[String]): Unit = {
    var A:Map[String,String] = Map()
    A+=("101"->"a1")
    A+=("102"->"a2")
    A+=("103"->"a3")
    A+=("104"->"a4")
    A+=("105"->"a5")
    A+=("106"->"a6")
    var B:Map[String,String] = Map()
    B+=("101"->"b1")
    B+=("102"->"b2")
    B+=("103"->"b3")
    B+=("104"->"b4")
    B+=("105"->"b5")

      A.foreach(a=>{
        var ak= a._1
        B.foreach(b=>{
          var bk=b._1
          if (ak==bk){
            var vaule=A.get(ak.toString()).getOrElse(Tuple1[String]("")).toString+","+B.get(bk.toString()).getOrElse(Tuple1[String]("a")).toString
            A+=(ak.toString()->vaule)
          }
        })
      })
    println(A)
  }
//Map(104 -> a4,b4, 103 -> a3,b3, 106 -> a6, 102 -> a2,b2, 101 -> a1,b1, 105 -> a5,b5)

11.两个表广播(只是借鉴)

 val sql = "SELECT * FROM " + FunctionEntity.getString("oracle.tableName")
    var mapValue: Map[String, String] = OracleConnect.getData(sql)
    var oracleValue: Broadcast[Map[String, String]] = ssc.sparkContext.broadcast(mapValue)
    
    val sqlTwo = "SELECT * FROM " + FunctionEntity.getString("oracle.tableNameTwo")
    var mapValueTwo: Map[String, String] = OracleConnect.getDataTwo(sqlTwo)
    var oracleValueTwo: Broadcast[Map[String, String]] = ssc.sparkContext.broadcast(mapValueTwo)

 var value: Map[String, String] = RepairEditionNine.analysisString(dataLineValue, 1)
              var fianlMap: Map[String, String] = OracleConnect.getRelationData(value, oracleValue.value)
              var fianlMapTwo: Map[String, String] = OracleConnect.getRelationDataTwo(fianlMap, oracleValueTwo.value)
              var put: Put = HbaseConnect.WriteHbase(fianlMapTwo)

12.这个是别人写的时间处理。写的比较多,可以使用:https://blog.csdn.net/springlustre/article/details/47273353

13.比较复杂的时间获取,例如今天周6,周7等:https://blog.csdn.net/qq_16038125/article/details/72834270

 

import java.text.SimpleDateFormat
import java.util.{Calendar, Date}

object T03 {
  def main(args: Array[String]): Unit = {
    //2018/12/13 13:50:16
    println(dayOfWeek("2018-12-23"))
    println(getNowTime())
  }

  //获取昨天日期
  def getNowTime():String={
    var  dateFormat:SimpleDateFormat = new SimpleDateFormat("yyyy-MM-dd")
    var cal:Calendar=Calendar.getInstance()
    cal.add(Calendar.DATE,-1)
    var yesterday=dateFormat.format(cal.getTime())
    yesterday
  }

  //获取本周一的日期
  def getNowWeekStart():String={
    var period:String=""
    var cal:Calendar =Calendar.getInstance();
    var df:SimpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
    cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY)
    //获取本周一的日期
    period=df.format(cal.getTime())
    period
  }

  //星期天数
  def dayOfWeek(dateStr: String): Int = {
    val sdf = new SimpleDateFormat("yyyy-MM-dd")
    val date = sdf.parse(dateStr)
    val cal = Calendar.getInstance();
    cal.setTime(date);
    var w = cal.get(Calendar.DAY_OF_WEEK)-1 ;
    //星期天 默认为7
    if (w <= 0)
      w = 7
    w
  }

}

 

posted @ 2018-11-12 20:04  聚云  阅读(1374)  评论(0编辑  收藏  举报