# -*- coding: utf-8 -*- """ author SparkByExamples.com """ from pyspark.sql import SparkSession # Create SparkSession spark = SparkSession.builder \ .appName('SparkByExamples.com') \ .getOrCreate() data=[["1"]] df=spark.createDataFrame(data,["id"]) from pyspark.sql.functions import * #current_date() & current_timestamp() df.withColumn("current_date",current_date()) \ .withColumn("current_timestamp",current_timestamp()) \ .show(truncate=False) #SQL spark.sql("select current_date(), current_timestamp()") \ .show(truncate=False) # Date & Timestamp into custom format df.withColumn("date_format",date_format(current_date(),"MM-dd-yyyy")) \ .withColumn("to_timestamp",to_timestamp(current_timestamp(),"MM-dd-yyyy HH mm ss SSS")) \ .show(truncate=False) #SQL spark.sql("select date_format(current_date(),'MM-dd-yyyy') as date_format ," + \ "to_timestamp(current_timestamp(),'MM-dd-yyyy HH mm ss SSS') as to_timestamp") \ .show(truncate=False)