如何拥有良好的日期格式?

时间:2019-01-24 14:20:41

标签: sql sql-server

我的sql表中有这种日期:// Log.e : Throwable error com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $ // MainActivity: public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final Handler handler = new Handler(); handler.postDelayed(new Runnable() { @Override public void run() { post_2(); } }, 300); } public void post_2(){ final ApiObject user = new ApiObject("a"); user.setValue("a"); ApiUtil.getServiceClass().setGetAllBody(user).enqueue(new Callback<MainResponse>(){ @Override public void onResponse(Call<MainResponse> call,Response<MainResponse> response){ if(response.isSuccessful()){ if(response.body().status==0){ Log.i("msg00", "Returned msg0 " + response.body().message); }else if (response.body().status==1){ Log.i("msg1", "Returned msg " + response.body().message); } //Log.i("msg", "Returned count " + postList.size()); } } @Override public void onFailure(Call<MainResponse>call,Throwable t) { Log.e("msg0","Throwable error "+t.toString()); } }); } } // interface RetrofitInterface public interface RetrofitInterface { @Headers("Content-Type: application/json") @POST("update.php") Call<MainResponse> setGetAllBody(@Body ApiObject user); } //ApiObject : public class ApiObject { @SerializedName("value") private String value; private String message; public ApiObject(String value) { this.value = value; } public void setValue(String value) { this.value = value; } public String getValue() { return value; } } // php file : <?php // include Marei DB Class include 'DB.php'; $data = file_get_contents("php://input"); $obj = json_decode($data); $db = DB::getInstance(); header('Content-Type', 'application/json; charset=utf-8'); $username= ""; if(!$_GET["value"]) { print json_encode(['status' => 0, 'message' => 'Username is Non !']); } else{ $username = $_GET["value"]; print json_encode(['status' => 0, 'message' => 'Username is Done !']); } ?>

我想用以下格式提取日期:1/24/2018 10:34:23 PM,也就是说我想拥有这个YYYYMMDD

我该怎么做?

我尝试过:

20180124

但是格式不正确...

我精确地说我的专栏名称是日期

3 个答案:

答案 0 :(得分:2)

使用以下内容以YYYYMMDD格式设置日期格式;

    SELECT
    CONVERT(VARCHAR(8), [DateField],112) AS DateField

答案 1 :(得分:2)

已根据要求添加为答案...

日期在SQL Server中没有格式,如何显示它们是格式。字符串具有格式。

根据您的版本,您可以使用FORMAT或CONVERT。

convert(varchar(8),yourColumn,112) 
format(yourCOlumn,'yyyyMMdd')

演示

declare @table table (yourColumn datetime)
insert into @table
values
(getdate())

select
    yourColumn
    ,format(yourColumn,'yyyyMMdd')
    ,convert(varchar(8),yourColumn,112)
from @table

如果您的列是字符串,则需要先将其转换为日期,或者仅以其他方式对其进行解析。

select 
    convert(varchar(8),cast('1/24/2018 10:34:23 PM' as datetime),112)
   ,format(cast('1/24/2018 10:34:23 PM' as datetime),'yyyyMMdd')

答案 2 :(得分:0)

我通常在下面的链接中提到已经在Stack Overflow上回答的问题。 How to convert DateTime to VarChar

Colin的答案确实对我有用,并且是参考传递时间的来源,我对SQL Server中的日期时间和格式感到困惑。